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

JSON schemas output references rather than primitive types for Unions #123

Closed
syastrov opened this issue Aug 9, 2020 · 0 comments · Fixed by #124
Closed

JSON schemas output references rather than primitive types for Unions #123

syastrov opened this issue Aug 9, 2020 · 0 comments · Fixed by #124

Comments

@syastrov
Copy link
Contributor

syastrov commented Aug 9, 2020

  • typical version: 2.0.28
  • Python version: 3.8.1
  • Operating System: macOS Catalina

Description

When trying to get the JSON schema for a TypedDict, some of the properties have a reference to a "#/definitions/NoneType" and "#/definitions/Str" (which doesn't exist) rather than using "type": "string" or "type": "null".

I assume the problem generalizes to all union types, not just Optionals.

Apparently, this only starts occurring when there are at least 3 fields that are Optionals on the type.

I have a PR forthcoming which should fix this.

What I Did

Ran the following script:

import json
from typing import TypedDict, Optional
import typic


class Foo(TypedDict):
    a: Optional[str]
    b: Optional[str]
    c: Optional[str]


print(json.dumps(typic.schema(Foo, primitive=True), indent=2))

Output

{
  "type": "object",
  "title": "Foo",
  "properties": {
    "b": {
      "title": "B",
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "c": {
      "title": "C",
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "a": {
      "title": "A",
      "anyOf": [
        {
          "$ref": "#/definitions/Str"
        },
        {
          "$ref": "#/definitions/NoneType"
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "b",
    "c",
    "a"
  ],
  "definitions": {}
}

All three properties should have had the same type:

      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
syastrov added a commit to syastrov/typical that referenced this issue Aug 9, 2020
…types in case of object with 3 or more unions.

Fixes seandstewart#123.
syastrov added a commit to syastrov/typical that referenced this issue Aug 9, 2020
…types in case of object with 3 or more unions.

Fixes seandstewart#123.
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 a pull request may close this issue.

1 participant