datamodel-code-generator
datamodel-code-generator copied to clipboard
KeyError at ref although specified in definitions
Describe the bug
Hello
When I start to create code from the json blow, I get a KeyError. The Error description says : KeyError: '#definitions/ChargingStationType' although ChargingStationType is specified under definitions. Has anyone an idea what the Error caused?
To Reproduce
Example schema:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"comment": "OCPP 2.0 - v1p0",
"definitions": {
"BootReasonEnumType": {
"type": "string",
"additionalProperties": true,
"enum": [
"ApplicationReset",
"FirmwareUpdate",
"LocalReset",
"PowerUp",
"RemoteReset",
"ScheduledReset",
"Triggered",
"Unknown",
"Watchdog"
]
},
"ChargingStationType": {
"javaType": "ChargingStation",
"type": "object",
"additionalProperties": true,
"properties": {
"serialNumber": {
"type": "string",
"maxLength": 20
},
"model": {
"type": "string",
"maxLength": 20
},
"modem": {
"$ref": "#definitions/ModemType"
},
"vendorName": {
"type": "string",
"maxLength": 50
},
"firmwareVersion": {
"type": "string",
"maxLength": 50
}
},
"required": [
"model",
"vendorName"
]
},
"ModemType": {
"javaType": "Modem",
"type": "object",
"additionalProperties": true,
"properties": {
"iccid": {
"type": "string",
"maxLength": 20
},
"imsi": {
"type": "string",
"maxLength": 20
}
}
}
},
"type": "object",
"additionalProperties": true,
"properties": {
"chargingStation": {
"$ref": "#definitions/ChargingStationType"
},
"reason": {
"$ref": "#definitions/BootReasonEnumType"
}
},
"required": [
"reason",
"chargingStation"
]
}
Used commandline:
$ datamodel-codegen --input test.json --input-file-type jsonschema --output test.py
Version:
- OS: ubuntu
- Python version: 3.9
- datamodel-code-generator version: 0.13.0
Additional context
Traceback (most recent call last):
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/__main__.py", line 541, in main
generate(
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/__init__.py", line 367, in generate
results = parser.parse()
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/base.py", line 449, in parse
self.parse_raw()
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1204, in parse_raw
self._parse_file(self.raw_obj, obj_name, path_parts)
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1277, in _parse_file
self.parse_obj(obj_name, root_obj, path_parts or ['#'])
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1164, in parse_obj
self.parse_object(name, obj, path)
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 599, in parse_object
fields=self.parse_object_fields(
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 545, in parse_object_fields
field_type = self.parse_item(modular_name, field, [*path, field_name])
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 639, in parse_item
return self.get_ref_data_type(item.ref)
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 435, in get_ref_data_type
reference = self.model_resolver.add_ref(ref)
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/reference.py", line 424, in add_ref
path = self.resolve_ref(ref)
File "/home/philipp/repos/scc/ocpp/trio-ocpp/.venv/lib/python3.9/site-packages/datamodel_code_generator/reference.py", line 369, in resolve_ref
ref: str = self.ids['/'.join(self.current_root)][joined_path]
KeyError: '#definitions/ChargingStationType'
I think you might be missing a leading slash.
Try to change
#definitions/ChargingStationType
to
#/definitions/ChargingStationType
Thanks @aktentasche if I change that, the KeyError does not appear. I had to add the slash to all references. In my use case, I have over 70 different schemas that are usually more complex than this one. (So more references) I would now have to take each one and adjust it and add the slash everywhere, that is a bit impractical. Is there no other solution to this?
@PJ-Schulz I'm sorry for my too-late reply. I think we need a slash in the position. Where do you get the schema?
no response