Describe the bug
When running datamodel-codegen
for any files that have additionalProperties
set to false
, the tool throws an exception. See the full debug output below:
Modified var:.. key = 'additionalProperties'
Modified var:.. model = False
13:26:28.368505 line 1792 obj = self.SCHEMA_OBJECT_TYPE.parse_obj(model)
13:26:28.368662 exception 1792 obj = self.SCHEMA_OBJECT_TYPE.parse_obj(model)
Exception:..... pydantic_core._pydantic_core.ValidationError: 1 validation error for JsonSchemaObject
Input should be a valid dictionary or instance of JsonSchemaObject [type=model_type, input_value=False, input_type=bool]
For further information visit https://errors.pydantic.dev/2.8/v/model_type
13:26:28.369075 line 1774 with self.root_id_context(raw):
13:26:28.369175 line 1770 with self.model_resolver.current_root_context(path_parts):
Call ended by exception
Elapsed time: 00:00:00.009855
13:26:28.369375 exception 1716 self._parse_file(self.raw_obj, obj_name, path_parts)
Exception:..... pydantic_core._pydantic_core.ValidationError: 1 validation error for JsonSchemaObject
Input should be a valid dictionary or instance of JsonSchemaObject [type=model_type, input_value=False, input_type=bool]
For further information visit https://errors.pydantic.dev/2.8/v/model_type
New var:....... self = <datamodel_code_generator.parser.jsonschema.JsonSchemaParser object at 0xffffbc9f1710>
New var:....... source = Source(path=PosixPath('cancelSandboxAuthorization.json'), text='{\n "$schema": "http://json-schema.org/draft-07/schema#",\n "$id": "cancelSandboxAuthorization.json",\n "definitions": {\n "relationships": {\n "type": "object",\n "properties": {\n "account": {\n "type": "object",\n "properties": {\n "data": {\n "type": "object",\n "properties": {\n "type": { "type": "string", "enum": ["depositAccount", "creditAccount"]},\n "id": { "$ref": "types.json#/definitions/identifier" }\n },\n "required": [ "type", "id" ],\n "additionalProperties": false\n }\n },\n "required": [ "data" ],\n "additionalProperties": false\n }\n },\n "required": [ "account" ],\n "additionalProperties": false\n },\n "authorization": {\n "title": "Cancel Simulated Authorization",\n "type": "object",\n "properties": {\n "type": { "type": "string", "const": "authorization" },\n "relationships": { "$ref": "#/definitions/relationships" }\n },\n "additionalProperties": false,\n "required": [\n "relationships"\n ]\n },\n "additionalProperties": false\n },\n\n "title": "Cancel Simulated Authorization",\n "type": "object",\n "properties": {\n "data": { "$ref": "#/definitions/authorization" }\n },\n "additionalProperties": false,\n "required": [ "data" ]\n}')
New var:....... path_parts = ['cancelSandboxAuthorization.json']
13:26:28.369428 exception 1699 yield source, path_parts
Exception:..... GeneratorExit
13:26:28.369499 line 1696 with self.model_resolver.current_base_path_context(
13:26:28.369529 line 1696 with self.model_resolver.current_base_path_context(
13:26:28.369589 return 1699 yield source, path_parts
Return value:.. None
Call ended by exception
Elapsed time: 00:00:53.387576
13:26:28.369662 exception 1236 self.parse_raw()
Exception:..... pydantic_core._pydantic_core.ValidationError: 1 validation error for JsonSchemaObject
Input should be a valid dictionary or instance of JsonSchemaObject [type=model_type, input_value=False, input_type=bool]
For further information visit https://errors.pydantic.dev/2.8/v/model_type
Call ended by exception
Elapsed time: 00:00:53.387761
Traceback (most recent call last):
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/datamodel_code_generator/__main__.py", line 449, in main
generate(
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/datamodel_code_generator/__init__.py", line 473, in generate
results = parser.parse()
^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/pysnooper/tracer.py", line 310, in simple_wrapper
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/datamodel_code_generator/parser/base.py", line 1236, in parse
self.parse_raw()
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/pysnooper/tracer.py", line 310, in simple_wrapper
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1716, in parse_raw
self._parse_file(self.raw_obj, obj_name, path_parts)
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/pysnooper/tracer.py", line 310, in simple_wrapper
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1792, in _parse_file
obj = self.SCHEMA_OBJECT_TYPE.parse_obj(model)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/pydantic/main.py", line 1135, in parse_obj
return cls.model_validate(obj)
^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/banking-core/.venv/lib/python3.11/site-packages/pydantic/main.py", line 568, in model_validate
return cls.__pydantic_validator__.validate_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for JsonSchemaObject
Input should be a valid dictionary or instance of JsonSchemaObject [type=model_type, input_value=False, input_type=bool]
For further information visit https://errors.pydantic.dev/2.8/v/model_type
The above example is from running the command on a directory, which can be found at https://www.unit.co/docs/api/#json-schema
The command used in this case:
datamodel-codegen --input json-schemas --input-file-type jsonschema --output my-schemas --output-model-type pydantic_v2.BaseModel
Simplified command provided below.
To Reproduce
Example schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "cancelSandboxAuthorization.json",
"definitions": {
"relationships": {
"type": "object",
"properties": {
"account": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"type": { "type": "string", "enum": ["depositAccount", "creditAccount"]},
"id": { "$ref": "types.json#/definitions/identifier" }
},
"required": [ "type", "id" ],
"additionalProperties": false
}
},
"required": [ "data" ],
"additionalProperties": false
}
},
"required": [ "account" ],
"additionalProperties": false
},
"authorization": {
"title": "Cancel Simulated Authorization",
"type": "object",
"properties": {
"type": { "type": "string", "const": "authorization" },
"relationships": { "$ref": "#/definitions/relationships" }
},
"additionalProperties": false,
"required": [
"relationships"
]
},
"additionalProperties": false
},
"title": "Cancel Simulated Authorization",
"type": "object",
"properties": {
"data": { "$ref": "#/definitions/authorization" }
},
"additionalProperties": false,
"required": [ "data" ]
}
Used commandline:
$ datamodel-codegen --input json-schemas/cancelSandboxAuthorization.json
Expected behavior
As described in the JSON Schema documentation, it's valid for additionalProperties
to be set as false
, and this should not cause an error.
Version:
β― uname -a Linux a59643f8c93b 6.6.32-linuxkit #1 SMP Thu Jun 13 14:13:01 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
3.11.9
0.25.8
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too