Skip to content

Commit eb465ad

Browse files
committed
Add --jsonschema-version and --openapi-version CLI options
1 parent eb92c8f commit eb465ad

5 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/datamodel_code_generator/_types/parser_config_dicts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
CollapseRootModelsNameStrategy,
2020
DataclassArguments,
2121
FieldTypeCollisionStrategy,
22+
JsonSchemaVersion,
2223
NamingStrategy,
2324
OpenAPIScope,
25+
OpenAPIVersion,
2426
ReadOnlyWriteOnlyModelType,
2527
ReuseScope,
2628
StrictTypes,
@@ -167,14 +169,15 @@ class GraphQLParserConfigDict(ParserConfigDict):
167169

168170

169171
class JSONSchemaParserConfigDict(ParserConfigDict):
170-
pass
172+
jsonschema_version: NotRequired[JsonSchemaVersion | None]
171173

172174

173175
class OpenAPIParserConfigDict(JSONSchemaParserConfigDict):
174176
openapi_scopes: NotRequired[list[OpenAPIScope] | None]
175177
include_path_parameters: NotRequired[bool]
176178
use_status_code_in_response_name: NotRequired[bool]
177179
openapi_include_paths: NotRequired[list[str] | None]
180+
openapi_version: NotRequired[OpenAPIVersion | None]
178181

179182

180183
ModelDict: TypeAlias = ParserConfigDict | GraphQLParserConfigDict | JSONSchemaParserConfigDict | OpenAPIParserConfigDict

src/datamodel_code_generator/arguments.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
FieldTypeCollisionStrategy,
2727
InputFileType,
2828
InputModelRefStrategy,
29+
JsonSchemaVersion,
2930
ModuleSplitMode,
3031
NamingStrategy,
3132
OpenAPIScope,
33+
OpenAPIVersion,
3234
ReadOnlyWriteOnlyModelType,
3335
ReuseScope,
3436
StrictTypes,
@@ -984,6 +986,24 @@ def start_section(self, heading: str | None) -> None:
984986
action="store_true",
985987
default=None,
986988
)
989+
openapi_options.add_argument(
990+
"--openapi-version",
991+
help="OpenAPI version to use (default: auto-detect from 'openapi' field). "
992+
"Use 'auto' for auto-detection, '3.0' for OpenAPI 3.0.x, '3.1' for OpenAPI 3.1.x.",
993+
choices=[v.value for v in OpenAPIVersion],
994+
default=None,
995+
)
996+
997+
# ======================================================================================
998+
# Options specific to JSON Schema input
999+
# ======================================================================================
1000+
base_options.add_argument(
1001+
"--jsonschema-version",
1002+
help="JSON Schema version to use (default: auto-detect from '$schema' field). "
1003+
"Use 'auto' for auto-detection, or specify a draft version explicitly.",
1004+
choices=[v.value for v in JsonSchemaVersion],
1005+
default=None,
1006+
)
9871007

9881008
# ======================================================================================
9891009
# Options specific to GraphQL input schemas

src/datamodel_code_generator/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
FieldTypeCollisionStrategy,
2323
GraphQLScope,
2424
InputFileType,
25+
JsonSchemaVersion,
2526
ModuleSplitMode,
2627
NamingStrategy,
2728
OpenAPIScope,
29+
OpenAPIVersion,
2830
ReadOnlyWriteOnlyModelType,
2931
ReuseScope,
3032
TargetPydanticVersion,
@@ -350,6 +352,8 @@ class GraphQLParserConfig(ParserConfig):
350352
class JSONSchemaParserConfig(ParserConfig):
351353
"""Configuration model for JsonSchemaParser.__init__()."""
352354

355+
jsonschema_version: JsonSchemaVersion | None = None
356+
353357

354358
class OpenAPIParserConfig(JSONSchemaParserConfig):
355359
"""Configuration model for OpenAPIParser.__init__()."""
@@ -358,6 +362,7 @@ class OpenAPIParserConfig(JSONSchemaParserConfig):
358362
include_path_parameters: bool = False
359363
use_status_code_in_response_name: bool = False
360364
openapi_include_paths: list[str] | None = None
365+
openapi_version: OpenAPIVersion | None = None
361366

362367

363368
class ParseConfig(BaseModel):

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,15 @@ def schema_paths(self) -> list[tuple[str, list[str]]]:
773773

774774
@cached_property
775775
def schema_features(self) -> JsonSchemaFeatures:
776-
"""Get schema features based on detected version."""
776+
"""Get schema features based on config or detected version."""
777777
from datamodel_code_generator.parser.schema_version import ( # noqa: PLC0415
778778
JsonSchemaFeatures,
779779
detect_jsonschema_version,
780780
)
781781

782+
config_version = getattr(self.config, "jsonschema_version", None)
783+
if config_version is not None and config_version != JsonSchemaVersion.Auto:
784+
return JsonSchemaFeatures.from_version(config_version)
782785
version = detect_jsonschema_version(self.raw_obj) if self.raw_obj else JsonSchemaVersion.Auto
783786
return JsonSchemaFeatures.from_version(version)
784787

src/datamodel_code_generator/parser/openapi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ class OpenAPIParser(JsonSchemaParser):
172172

173173
@cached_property
174174
def schema_features(self) -> OpenAPISchemaFeatures:
175-
"""Get schema features based on detected OpenAPI version."""
175+
"""Get schema features based on config or detected OpenAPI version."""
176176
from datamodel_code_generator.parser.schema_version import ( # noqa: PLC0415
177177
OpenAPISchemaFeatures,
178178
detect_openapi_version,
179179
)
180180

181+
config_version = getattr(self.config, "openapi_version", None)
182+
if config_version is not None and config_version != OpenAPIVersion.Auto:
183+
return OpenAPISchemaFeatures.from_openapi_version(config_version)
181184
version = detect_openapi_version(self.raw_obj) if self.raw_obj else OpenAPIVersion.Auto
182185
return OpenAPISchemaFeatures.from_openapi_version(version)
183186

0 commit comments

Comments
 (0)