Skip to content

feat: basic support for openapi 3.1#109

Merged
mnahkies merged 2 commits into
mainfrom
mn/feat/basic-openapi31-support
Nov 12, 2023
Merged

feat: basic support for openapi 3.1#109
mnahkies merged 2 commits into
mainfrom
mn/feat/basic-openapi31-support

Conversation

@mnahkies
Copy link
Copy Markdown
Owner

@mnahkies mnahkies commented Nov 12, 2023

this adds basic support for openapi 3.1 definitions, based on the migration blog post from openapis.org.

the primary change is allowing null as a type, and that type can be an array.

the other changes listed relating to file uploads, and exclusiveMinimum aren't applicable as these aren't really supported at all yet (#51, #53)

there's probably a bunch of other gaps in general JSON schema support, such as the if / else things mentioned, but there's relatively few examples of complex 3.1.0 definitions to test against.

I stumbled across https://github.com/APIs-guru/openapi-directory looking for samples and I've tested these changes against these definitions:

It appears to be giving reasonable output - no compile errors at least, and nothing obviously wrong doing a quick scan of output.

ref: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0

mnahkies added a commit that referenced this pull request Nov 12, 2023
previously, these would never be marked as optional, even if the key
they belonged to wasn't `required`

this was made evident whilst testing #109
which relies on `oneOf` to handle arrays of `type`

this moves that logic up into the AbstractSchemaBuilder, and applies it more predictably,
as well as swapping the order such that `nullable` always comes before `optional`
@mnahkies mnahkies force-pushed the mn/feat/basic-openapi31-support branch from ac7761d to bddc979 Compare November 12, 2023 16:13
mnahkies added a commit that referenced this pull request Nov 12, 2023
previously, these would never be marked as optional, even if the key
they belonged to wasn't `required`

this was made evident whilst testing
#109
which relies on `oneOf` to handle arrays of `type`

this moves that logic up into the `AbstractSchemaBuilder`, and applies
it more predictably,
as well as swapping the order such that `nullable` always comes before
`optional`

this means that schemas like:
```
schema:
  type: object
  properties:
    enabled:
      "$ref": "#/components/schemas/actions-enabled"
    allowed_actions:
      "$ref": "#/components/schemas/allowed-actions"
  required:
    - enabled
```
will correctly mark `allowed_actions` as optional, and schemas like:
```
setup_intent_payment_method_options:
  description: ''
  properties:
    us_bank_account:
      anyOf:
        - $ref: >-
            #/components/schemas/setup_intent_payment_method_options_us_bank_account
        - $ref: >-
            #/components/schemas/setup_intent_type_specific_payment_method_options_client
  title: SetupIntentPaymentMethodOptions
  type: object
```
correctly mark `us_bank_account` as optional.

note: the typescript types were already correct here. the problem wasn't
picked up by typescript because were being stricter on the validation
and then passing that to a looser type, eg: passing `T` to a `T |
undefined` parameter.
this adds basic support for openapi 3.1 definitions, based on the
mgiration blog post from openapis.org.

the primary change is allowing `null` as a `type`, and that `type`
can be an array.

the other changes listed relating to file uploads, and
`exclusiveMinimum` aren't applicable as these aren't really supported at
all yet (#51,
#53)

there's probably a bunch of other gaps in general JSON schema support,
such as the `if` / `else` things mentioned, but there's relatively few
examples of complex `3.1.0` definitions to test against.

I stumbled across https://github.com/APIs-guru/openapi-directory looking
for samples and I've tested these changes against these definitions:
- https://github.com/APIs-guru/openapi-directory/blob/dec74da7a6785d5d5b83bc6a4cebc07336d67ec9/APIs/vercel.com/0.0.1/openapi.yaml
- https://github.com/APIs-guru/openapi-directory/blob/dec74da7a6785d5d5b83bc6a4cebc07336d67ec9/APIs/discourse.local/latest/openapi.yaml
- https://github.com/APIs-guru/openapi-directory/blob/dec74da7a6785d5d5b83bc6a4cebc07336d67ec9/APIs/adyen.com/CheckoutService/70/openapi.yaml

It appears to be giving reasonable output - no compile errors at
least, and nothing obviously wrong doing a quick scan of output.

ref: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0
@mnahkies mnahkies force-pushed the mn/feat/basic-openapi31-support branch from bddc979 to 7073d5d Compare November 12, 2023 16:23
@mnahkies mnahkies marked this pull request as ready for review November 12, 2023 16:26
@mnahkies mnahkies enabled auto-merge (squash) November 12, 2023 16:27
@mnahkies mnahkies merged commit 60838d9 into main Nov 12, 2023
@mnahkies mnahkies deleted the mn/feat/basic-openapi31-support branch November 12, 2023 16:27
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.

1 participant