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 Schema generator doesn't honor default_factory #114

Closed
mishamsk opened this issue Jun 11, 2023 · 2 comments · Fixed by #122
Closed

JSON Schema generator doesn't honor default_factory #114

mishamsk opened this issue Jun 11, 2023 · 2 comments · Fixed by #122
Labels
bug Something isn't working

Comments

@mishamsk
Copy link
Contributor

  • mashumaro version: 3.7
  • Python version: 3.11 (but 3.10 is the same)
  • Operating System: macOS Ventura

Description

See the full code and output below. When using default value, directly or via field(default=...) the schema correctly recognizes the field as not required. But it doesn't work with default_factory unfortunately.

Shall I open a PR?

What I Did

import json
from dataclasses import dataclass, field
from functools import partial

from mashumaro import DataClassDictMixin
from mashumaro.jsonschema import build_json_schema


@dataclass
class Foo(DataClassDictMixin):
    bar: frozenset[str] = field(default_factory=frozenset)


print(build_json_schema(Foo).to_json(encoder=partial(json.dumps, indent=2)))
# {
#   "type": "object",
#   "title": "Foo",
#   "properties": {
#     "bar": {
#       "type": "array",
#       "items": {
#         "type": "string"
#       },
#       "uniqueItems": true
#     }
#   },
#   "additionalProperties": false,
#   "required": [
#     "bar"
#   ]
# }

@mishamsk mishamsk changed the title JSON Schema generate doesn't honor default_factory JSON Schema generator doesn't honor default_factory Jun 11, 2023
@Fatal1ty
Copy link
Owner

@mishamsk

Thank you for paying attention to this! Yes, please, go ahead and open a PR. I will review it as soon as possible.

@mishamsk
Copy link
Contributor Author

@Fatal1ty #122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants