-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds features to example
functionality
#603
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -402,6 +402,10 @@ | |
def example(%Schema{type: :string, format: :"date-time"}), do: "2020-04-20T16:20:00Z" | ||
def example(%Schema{type: :string, format: :uuid}), do: "02ef9c5f-29e6-48fc-9ec3-7ed57ed351f6" | ||
|
||
def example(%Schema{type: :string, pattern: regexp}) when is_binary(regexp), do: | ||
Randex.stream(Regex.compile!(regexp)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this change to generate examples matching the schema, it must also take into account the |
||
|> Enum.take(1) | ||
|> List.first() | ||
def example(%Schema{type: :string}), do: "" | ||
def example(%Schema{type: :integer} = s), do: example_for(s, :integer) | ||
def example(%Schema{type: :number} = s), do: example_for(s, :number) | ||
|
@@ -468,7 +472,7 @@ | |
# Only handles :object schemas for now | ||
schemas | ||
|> Enum.map(&example/1) | ||
|> Enum.reduce(%{}, &Map.merge/2) | ||
|> Enum.reduce(%{}, &any_or_all_of/2) | ||
end | ||
|
||
defp example_for(%{minimum: min} = schema, type) | ||
|
@@ -498,9 +502,12 @@ | |
defp example_for(schemas, type, all_schemas) when type in [:anyOf, :allOf] do | ||
schemas | ||
|> Enum.map(&example(&1, all_schemas)) | ||
|> Enum.reduce(%{}, &Map.merge/2) | ||
|> Enum.reduce(%{}, &any_or_all_of/2) | ||
end | ||
|
||
defp any_or_all_of(l,r) when is_map(l), do: &Map.merge(&1, &2) | ||
Check warning on line 508 in lib/open_api_spex/schema.ex
|
||
defp any_or_all_of(l,r) when is_binary(l), do: l | ||
Check warning on line 509 in lib/open_api_spex/schema.ex
|
||
|
||
defp default(schema_module) when is_atom(schema_module), do: schema_module.schema().default | ||
defp default(%{default: default}), do: default | ||
defp default(%Reference{}), do: nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
randex
is an optional dependency, then this should have some runtime check for theRandex
module being loaded?