Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/open_api_spex/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Collaborator

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 the Randex module being loaded?

Copy link
Contributor

@zorbash zorbash May 21, 2024

Choose a reason for hiding this comment

The 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 minLength and maxLength properties. Does Randex support such parameters?

|> 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Test (OTP 25 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 508 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Test (OTP 23 / Elixir 1.12)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 508 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Test (OTP 23 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 508 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 25 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)
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

View workflow job for this annotation

GitHub Actions / Test (OTP 25 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 509 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Test (OTP 23 / Elixir 1.12)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 509 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Test (OTP 23 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 509 in lib/open_api_spex/schema.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 25 / Elixir 1.13)

variable "r" is unused (if the variable is not meant to be used, prefix it with an underscore)

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
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ defmodule OpenApiSpex.Mixfile do
{:dialyxir, "~> 1.0", only: [:dev], runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:jason, "~> 1.0", optional: true},
{:randex, "~> 0.4", optional: true},
{:phoenix, "~> 1.3", only: [:dev, :test]},
{:plug, "~> 1.7"},
{:poison, "~> 3.0 or ~> 4.0 or ~> 5.0", optional: true},
Expand Down