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

fix(OpenApiType): Use anyOf instead of oneOf for conflicting types #118

Merged
merged 1 commit into from
Apr 10, 2024

Conversation

provokateurin
Copy link
Member

The current assumption that a union type is equivalent to oneOf is not correct.

The PHP type annotation

array{a: string}|array{a: string, b: int}

would mean the JSON would always be either

{"a": ""}

or

{"a": "", "b": 0}

The OpenAPI spec mandates that exactly ONE schema matches the data. No more no less.
Our generated spec would look like

{
  "oneOf": [
    {
      "type": "object"
      "properties": {
        "a": {
          "type": "string"
        }
      }
    },
    {
      "type": "object"
      "properties": {
        "a": {
          "type": "string"
        }
        "b": {
          "type": "integer"
        }
      }
    }
  ]
}

The first schema would always match both possibilities (additional properties are always allowed and ignored), so the assumption that only one schema ever matches is not true.

We don't want to use anyOf all the time though, because it makes some cases harder e.g. when you have string|int because here the assumption is correct.

@provokateurin provokateurin force-pushed the fix/openapitype/anyof-conflicting-types branch from 6ec3c25 to 4b13023 Compare April 10, 2024 11:28
@provokateurin provokateurin force-pushed the fix/openapitype/anyof-conflicting-types branch from 4b13023 to dc5b7d4 Compare April 10, 2024 11:59
@provokateurin provokateurin force-pushed the fix/openapitype/anyof-conflicting-types branch from dc5b7d4 to 61f492c Compare April 10, 2024 12:47
@provokateurin provokateurin merged commit 57e66f0 into main Apr 10, 2024
12 checks passed
@provokateurin provokateurin deleted the fix/openapitype/anyof-conflicting-types branch April 10, 2024 13:43
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.

3 participants