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

Add Tuple serialization #11

merged 2 commits into from
Apr 13, 2021

Conversation

elbywan
Copy link
Owner

@elbywan elbywan commented Apr 12, 2021

Purpose

Adds the self.to_openapi_schema for the Tuple type.

@dukeraphaelng This should solve #9, let me know if this looks good to you I'll merge afterwards.

Note

OpenAPI v3.0.x does not support the proper way to express tuples as a json schema.

Passing an array of items is forbidden by the specification:

As a workaround this PR serializes tuples into an array with fixed bounds and items that can be one of the tuple types.

Example

puts Tuple(Int32, String, Tuple(Bool | Array(Float64))).to_openapi_schema.to_pretty_json
{
  "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"
            }
          ]
        }
      }
    ]
  }
}

@dukenguyenxyz
Copy link
Contributor

this is awesome! thank you so much

@elbywan elbywan merged commit c48cc58 into master Apr 13, 2021
@elbywan elbywan deleted the serialize-tuples branch April 13, 2021 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants