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 some cases of unsatisfiable schemas when flattening enums #325

Merged
merged 7 commits into from
Aug 22, 2024

Conversation

GREsau
Copy link
Owner

@GREsau GREsau commented Aug 21, 2024

Addresses #165

With this change, the output from the example in #165 is:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "MyStruct",
  "type": "object",
  "allOf": [
    {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "enum1variant": {
              "type": "string"
            }
          },
          "required": [
            "enum1variant"
          ]
        }
      ]
    },
    {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "enum2variant": {
              "type": "string"
            }
          },
          "required": [
            "enum2variant"
          ]
        }
      ]
    }
  ]
}

Note that it correctly contains both enums, and neither of them have additionalProperties

GREsau added 4 commits August 21, 2024 19:31
`unevaluatedProperties` matches the desired behaviour in more cases, fixing some more cases of unsatisifable schemas.
But convert to `additionalProperties` for old JSON Schema versions, bringing nested properties into top-level
@GREsau GREsau force-pushed the flatten-enum-additionalProperties branch from 3007ec6 to 1e65da8 Compare August 22, 2024 15:31
@GREsau GREsau force-pushed the flatten-enum-additionalProperties branch from 1e65da8 to 82ca2db Compare August 22, 2024 15:36
@GREsau
Copy link
Owner Author

GREsau commented Aug 22, 2024

This PR now also addresses #164 - the output from that issue's example is:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "property": {
      "type": "string"
    }
  },
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "variant": {
          "type": "string"
        }
      },
      "required": [
        "variant"
      ]
    }
  ],
  "required": [
    "property"
  ],
  "unevaluatedProperties": false
}

Or when using draft-07 settings:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "property": {
      "type": "string"
    },
    "variant": true
  },
  "required": [
    "property"
  ],
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "variant": {
          "type": "string"
        }
      },
      "required": [
        "variant"
      ]
    }
  ],
  "additionalProperties": false
}

@GREsau
Copy link
Owner Author

GREsau commented Aug 22, 2024

Still to do: improve test coverage - could borrow the types from the linked issues. Also include tests for draft-07/openapi3

@GREsau GREsau merged commit 9658c42 into master Aug 22, 2024
8 checks passed
@GREsau GREsau deleted the flatten-enum-additionalProperties branch August 22, 2024 18:56
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