Skip to content

fix: optional oneOf / allOf / anyOf / $ref's#110

Merged
mnahkies merged 2 commits intomainfrom
mn/fix/optionalOneOf
Nov 12, 2023
Merged

fix: optional oneOf / allOf / anyOf / $ref's#110
mnahkies merged 2 commits intomainfrom
mn/fix/optionalOneOf

Conversation

@mnahkies
Copy link
Copy Markdown
Owner

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.

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 enabled auto-merge (squash) November 12, 2023 16:12
@mnahkies mnahkies merged commit 2ff114a into main Nov 12, 2023
@mnahkies mnahkies deleted the mn/fix/optionalOneOf branch November 12, 2023 16:16
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