Describe the bug
I have a schema defined across two files, main.schema.json
and dummy.schema.json
.
I want to generate a Python module called autogen
, structured like so:
autogen/
βββ __init__.py <-- Main class lives here
βββ dummy.py <-- Dummy class lives here
This works fine without the --use-title-as-name
flag, but the class defined in dummy.py
is called Schema
, instead of taking the name defined in dummy.schema.json["title"]
. That's to be expected when not supplying the --use-title-as-name
flag.
If, however I use the --use-title-as-name
flag, all of the code gets placed in a single file!
To Reproduce
Example schema:
File main.schema.json
:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Main",
"description": "The Root of the data structure.",
"properties": {
"dummy": {
"description": "A list of dummy objects.",
"type": "array",
"title": "Dummy", <--- Tried with/without, does not make a difference
"items": {
"type": "object",
"title": "Dummy", <--- Tried with/without, does not make a difference
"$ref": "./dummy.schema.json"
}
}
}
}
File dummy.schema.json
:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Dummy",
"description": "An dummy class to demonstrate this bug.",
"required": ["field_0", "field_1"],
"properties": {
"field_0": {
"type": "string"
},
"field_1": {
"type": ["string", "null"],
"default": null,
"description": "Another field."
}
}
}
Used bash script:
#!/usr/bin/bash
SCHEMA_PATH=$(dirname $0)/main.schema.json
OUT_PATH=$(dirname $0)/autogen/
datamodel-codegen \
--input $SCHEMA_PATH \
--input-file-type jsonschema \
--output-model-type dataclasses.dataclass \
--target-python-version 3.8 \
--set-default-enum-member \
--capitalise-enum-members \
--use-subclass-enum \
--snake-case-field \
--use-schema-description \
--enable-version-header \
--output $OUT_PATH \
--reuse-model \
--use-double-quotes \
--use-title-as-name
Expected behavior
I expect:
"title"
field in the (sub)schema.Version:
Additional context
I think I provided everything, but let me know if you need more info.
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