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

Empty tuple type does not generate valid JSON schema #2496

Closed
3 tasks done
ghost opened this issue Mar 8, 2021 · 2 comments · Fixed by #2497
Closed
3 tasks done

Empty tuple type does not generate valid JSON schema #2496

ghost opened this issue Mar 8, 2021 · 2 comments · Fixed by #2497
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@ghost
Copy link

ghost commented Mar 8, 2021

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

 python -c "import pydantic.utils; print(pydantic.utils.version_info())"
             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /Users/smacpher/.virtualenvs/nucleus/lib/python3.6/site-packages/pydantic
               python version: 3.6.9 (default, Oct 16 2019, 11:14:39)  [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)]
                     platform: Darwin-17.7.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']
import jsonschema
import pydantic

from typing import Tuple

class Model(pydantic.BaseModel):
    f: Tuple[()]

jsonschema.Draft7Validator.check_schema(Model.schema())

This fails with:

SchemaError: [] is not valid under any of the given schemas

Failed validating 'anyOf' in metaschema['properties']['properties']['additionalProperties']['properties']['items']:
    {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
     'default': True}

On schema['properties']['f']['items']:
    []

The generated schema is:

{'title': 'Model',
 'type': 'object',
 'properties': {'f': {'title': 'F', 'type': 'array', 'items': []}},
 'required': ['f']}

Is this expected behavior? Would the desired behavior be to set maxItems: 0 and remove items: [] like such (that would make the generated schema valid)?

{'title': 'Model',
 'type': 'object',
 'properties': {'f': {'title': 'F', 'type': 'array', 'maxItems': 0}},
 'required': ['f']}

BTW, glad to see that empty tuples (Tuple[()]) are now supported (#2318)! Thank you 🙏 .

@ghost ghost added the bug V1 Bug related to Pydantic V1.X label Mar 8, 2021
@PrettyWood
Copy link
Member

Hi @smacpher-myst and thanks a lot for reporting!
I opened #2497 and tackled your issue at the same time. Feedback welcome :)

@ghost
Copy link
Author

ghost commented Mar 8, 2021

@PrettyWood no problem! Awesome, thanks for the super quick turn around :D. Looked over your changes and they look good to me – approved as well (FWIW).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant