Support generating models from json-schemas defined in multiple files
See original GitHub issueIs your feature request related to a problem? Please describe. Now the tool cli API supports only single file as input. I’d suggest to extend it to have directory as as option. This would help with generating models from json-schema (or may be even from other formats as well). For example, let’s have the following schemas (see below).
Since the tool doesn’t support directory as input I make a separate CLI call for both file. In that case the output would have:
- model generated from object.json for Object
- model generated from object.json for referenced Metadata
- model generated from metadata.json
Even more, if we have 2 and more files, which, like object.json refer to the same external reference, we will have copy of the model in every output file.
// object.json
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"title": "Object",
"properties": {
"metadata": {
"$ref": "metadata.json#/"
}
},
"required": [
"metadata",
]
}
// another file in the same dir
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"title": "Object",
"properties": {
"somefield": {
"type": "integer"
}
},
"required": [
"metadata",
]
}
Describe the solution you’d like In the described case I would like to avoid duplicated models output for the references.
Describe alternatives you’ve considered May be, another alternative is to recursively call the datamodel-codegen in case of reference to the external file (then even if we had made few calls to generate the same reference from different referees we would just override the previous result in a separate file, instead of generating inline pydantic model-duplicate).
Additional context Add any other context or screenshots about the feature request here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:37 (22 by maintainers)
Top GitHub Comments
@koxudaxi Thanks a lot for the heads up, thats very kind 🙏 ! I’ll make the changes like you suggested and will test it soon with this new version.
That is amazing, works really well now. Thank you!