-
Notifications
You must be signed in to change notification settings - Fork 203
feat: Add OTP 28+ compatibility with version-aware regex pattern handling #670
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
Open
matthewsinclair
wants to merge
2
commits into
open-api-spex:master
Choose a base branch
from
matthewsinclair:fix/otp-28-compatibility-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,101 +184,60 @@ defmodule OpenApiSpex do | |
Error.message(error) | ||
end | ||
|
||
@doc """ | ||
Declares a struct based `OpenApiSpex.Schema` | ||
|
||
- defines the schema/0 callback | ||
- ensures the schema is linked to the module by "x-struct" extension property | ||
- defines a struct with keys matching the schema properties | ||
- defines a @type `t` for the struct | ||
- derives a `Jason.Encoder` and/or `Poison.Encoder` for the struct | ||
|
||
See `OpenApiSpex.Schema` for additional examples and details. | ||
|
||
## Example | ||
|
||
require OpenApiSpex | ||
|
||
defmodule User do | ||
OpenApiSpex.schema %{ | ||
title: "User", | ||
description: "A user of the app", | ||
type: :object, | ||
properties: %{ | ||
id: %Schema{type: :integer, description: "User ID"}, | ||
name: %Schema{type: :string, description: "User name", pattern: ~r/[a-zA-Z][a-zA-Z0-9_]+/}, | ||
email: %Schema{type: :string, description: "Email address", format: :email}, | ||
inserted_at: %Schema{type: :string, description: "Creation timestamp", format: :'date-time'}, | ||
updated_at: %Schema{type: :string, description: "Update timestamp", format: :'date-time'} | ||
}, | ||
required: [:name, :email], | ||
example: %{ | ||
"id" => 123, | ||
"name" => "Joe User", | ||
"email" => "[email protected]", | ||
"inserted_at" => "2017-09-12T12:34:55Z", | ||
"updated_at" => "2017-09-13T10:11:12Z" | ||
} | ||
} | ||
end | ||
|
||
## Example | ||
|
||
This example shows the `:struct?` and `:derive?` options that may | ||
be passed to `schema/2`: | ||
|
||
defmodule MyAppWeb.Schemas.User do | ||
require OpenApiSpex | ||
alias OpenApiSpex.Schema | ||
|
||
OpenApiSpex.schema( | ||
%{ | ||
type: :object, | ||
properties: %{ | ||
name: %Schema{type: :string} | ||
} | ||
}, | ||
struct?: false, | ||
derive?: false | ||
) | ||
end | ||
|
||
## Options | ||
@doc false | ||
def should_use_runtime_compilation?(body) do | ||
with true <- System.otp_release() >= "28", | ||
true <- Schema.has_regex_pattern?(body) do | ||
true | ||
else | ||
_ -> false | ||
end | ||
end | ||
|
||
- `:struct?` (boolean) - When false, prevents the automatic generation | ||
of a struct definition for the schema module. | ||
- `:derive?` (boolean) When false, prevents the automatic generation | ||
of a `@derive` call for either `Poison.Encoder` | ||
or `Jason.Encoder`. Using this option can | ||
prevent "... protocol has already been consolidated ..." | ||
compiler warnings. | ||
""" | ||
defmacro schema(body, opts \\ []) do | ||
quote do | ||
@compile {:report_warnings, false} | ||
@behaviour OpenApiSpex.Schema | ||
@schema OpenApiSpex.build_schema( | ||
unquote(body), | ||
Keyword.merge([module: __MODULE__], unquote(opts)) | ||
) | ||
|
||
schema = OpenApiSpex.build_schema(unquote(body), Keyword.merge([module: __MODULE__], unquote(opts))) | ||
|
||
case OpenApiSpex.should_use_runtime_compilation?(unquote(body)) do | ||
true -> | ||
IO.warn(""" | ||
[OpenApiSpex] Regex patterns in schema definitions are deprecated in OTP 28+. | ||
Consider using string patterns: pattern: "\\\\d-\\\\d" instead of pattern: ~r/\\\\d-\\\\d/ | ||
""", Macro.Env.stacktrace(__ENV__)) | ||
|
||
def schema do | ||
OpenApiSpex.build_schema_without_validation(unquote(body), Keyword.merge([module: __MODULE__], unquote(opts))) | ||
end | ||
|
||
false -> | ||
@schema schema | ||
def schema, do: @schema | ||
end | ||
|
||
unless Module.get_attribute(__MODULE__, :moduledoc) do | ||
@moduledoc [@schema.title, @schema.description] | ||
@moduledoc [schema.title, schema.description] | ||
|> Enum.reject(&is_nil/1) | ||
|> Enum.join("\n\n") | ||
end | ||
|
||
def schema, do: @schema | ||
|
||
if Map.get(@schema, :"x-struct") == __MODULE__ do | ||
if Keyword.get(unquote(opts), :derive?, true) do | ||
@derive Enum.filter([Poison.Encoder, Jason.Encoder], &Code.ensure_loaded?/1) | ||
end | ||
|
||
if Keyword.get(unquote(opts), :struct?, true) do | ||
defstruct Schema.properties(@schema) | ||
@type t :: %__MODULE__{} | ||
end | ||
case Map.get(schema, :"x-struct") == __MODULE__ do | ||
true -> | ||
case Keyword.get(unquote(opts), :derive?, true) do | ||
true -> @derive Enum.filter([Poison.Encoder, Jason.Encoder], &Code.ensure_loaded?/1) | ||
false -> nil | ||
end | ||
|
||
case Keyword.get(unquote(opts), :struct?, true) do | ||
true -> | ||
defstruct Schema.properties(schema) | ||
@type t :: %__MODULE__{} | ||
false -> nil | ||
end | ||
|
||
false -> nil | ||
end | ||
end | ||
end | ||
|
@@ -328,6 +287,27 @@ defmodule OpenApiSpex do | |
schema | ||
end | ||
|
||
@doc false | ||
def build_schema_without_validation(body, opts \\ []) do | ||
module = opts[:module] || body[:"x-struct"] | ||
|
||
attrs = | ||
body | ||
|> Map.delete(:__struct__) | ||
|> update_in([:"x-struct"], fn struct_module -> | ||
if Keyword.get(opts, :struct?, true) do | ||
struct_module || module | ||
else | ||
struct_module | ||
end | ||
end) | ||
|> update_in([:title], fn title -> | ||
title || title_from_module(module) | ||
end) | ||
|
||
struct(OpenApiSpex.Schema, attrs) | ||
end | ||
|
||
def title_from_module(nil), do: nil | ||
|
||
def title_from_module(module) do | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think this should be a public function. It's also hijacking the
@doc
fordefmacro schema/2
.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.
I'll submit a change to fix this, but that method is called from elsewhere, so I think it needs to be public, no? Unless I'm missing something. The @doc issue is easier to fix (I think). :)