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

Add Tuple serialization #11

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/amber/helper_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ describe OpenAPI::Generator::Helpers::Amber do
- union_types
- free_form
- array_of_hash
- tuple
type: object
properties:
union_types:
Expand All @@ -219,6 +220,23 @@ describe OpenAPI::Generator::Helpers::Amber do
oneOf:
- type: integer
- type: string
tuple:
maxItems: 3
minItems: 3
type: array
items:
oneOf:
- type: integer
- type: string
- maxItems: 1
minItems: 1
type: array
items:
oneOf:
- type: array
items:
type: number
- type: boolean
AmberSpec_Payload:
required:
- hello
Expand Down
18 changes: 18 additions & 0 deletions spec/core/generator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe OpenAPI::Generator do
- union_types
- free_form
- array_of_hash
- tuple
type: object
properties:
union_types:
Expand All @@ -174,6 +175,23 @@ describe OpenAPI::Generator do
oneOf:
- type: integer
- type: string
tuple:
maxItems: 3
minItems: 3
type: array
items:
oneOf:
- type: integer
- type: string
- maxItems: 1
minItems: 1
type: array
items:
oneOf:
- type: array
items:
type: number
- type: boolean
responses: {}
parameters: {}
examples: {}
Expand Down
18 changes: 18 additions & 0 deletions spec/lucky/helper_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe OpenAPI::Generator::Helpers::Lucky do
- union_types
- free_form
- array_of_hash
- tuple
type: object
properties:
union_types:
Expand All @@ -170,6 +171,23 @@ describe OpenAPI::Generator::Helpers::Lucky do
oneOf:
- type: integer
- type: string
tuple:
maxItems: 3
minItems: 3
type: array
items:
oneOf:
- type: integer
- type: string
- maxItems: 1
minItems: 1
type: array
items:
oneOf:
- type: array
items:
type: number
- type: boolean
LuckySpec_Payload:
required:
- hello
Expand Down
37 changes: 36 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ struct Model
property union_types : Int32 | String | Hash(String, InnerModel)
property free_form : JSON::Any
property array_of_hash : Array(Hash(String, Int32 | String))
property tuple : Tuple(Int32, String, Tuple(Bool | Array(Float64)))

SCHEMA = <<-JSON
{
"required": [
"union_types",
"free_form",
"array_of_hash"
"array_of_hash",
"tuple"
],
"type": "object",
"properties": {
Expand Down Expand Up @@ -124,6 +126,39 @@ struct Model
]
}
}
},
"tuple": {
"maxItems": 3,
"minItems": 3,
"type": "array",
"items": {
"oneOf": [
{
"type": "integer"
},
{
"type": "string"
},
{
"maxItems": 1,
"minItems": 1,
"type": "array",
"items": {
"oneOf": [
{
"type": "array",
"items": {
"type": "number"
}
},
{
"type": "boolean"
}
]
}
}
]
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions spec/spider-gazelle/helper_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ describe OpenAPI::Generator::Helpers::ActionController do
- union_types
- free_form
- array_of_hash
- tuple
type: object
properties:
union_types:
Expand All @@ -226,6 +227,23 @@ describe OpenAPI::Generator::Helpers::ActionController do
oneOf:
- type: integer
- type: string
tuple:
maxItems: 3
minItems: 3
type: array
items:
oneOf:
- type: integer
- type: string
- maxItems: 1
minItems: 1
type: array
items:
oneOf:
- type: array
items:
type: number
- type: boolean
ActionControllerSpec_Payload:
required:
- mandatory
Expand Down
32 changes: 32 additions & 0 deletions src/openapi-generator/extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ class Array(T)
end
end

# :nodoc:
# Define a `self.to_openapi_schema` method for the Tuple class.
#
# OpenAPI 3.0 does not support tuples (3.1 does), so we serialize it into a fixed bounds array.
# see: https://github.com/OAI/OpenAPI-Specification/issues/1026
struct Tuple
def self.to_openapi_schema
schema_items = uninitialized OpenAPI::Schema | OpenAPI::Reference

{% begin %}
{% types = [] of Types %}
{% for i in 0...T.size %}
{% for t in T[i].union_types %}
{% types << t %}
{% end %}
{% end %}

::OpenAPI::Generator::Serializable.generate_schema(
schema_items,
types: {{ types }},
)
{% end %}

OpenAPI::Schema.new(
type: "array",
items: schema_items,
min_items: {{ T.size }},
max_items: {{ T.size }}
)
end
end

# :nodoc:
# Define a `self.to_openapi_schema` method for the Hash class.
class Hash(K, V)
Expand Down