6565from datamodel_code_generator .parser import DefaultPutDict , LiteralType
6666
6767if TYPE_CHECKING :
68- from datamodel_code_generator ._types import GraphQLParserConfigDict , OpenAPIParserConfigDict , ParserConfigDict
68+ from datamodel_code_generator ._types import (
69+ GraphQLParserConfigDict ,
70+ JSONSchemaParserConfigDict ,
71+ OpenAPIParserConfigDict ,
72+ ParserConfigDict ,
73+ )
6974 from datamodel_code_generator ._types .generate_config_dict import GenerateConfigDict
7075 from datamodel_code_generator .config import GenerateConfig , ParserConfig
7176
@@ -456,7 +461,10 @@ def _build_module_content(
456461def _create_parser_config (
457462 config_class : type [_ConfigT ],
458463 generate_config : GenerateConfig ,
459- additional_options : ParserConfigDict | OpenAPIParserConfigDict | GraphQLParserConfigDict ,
464+ additional_options : ParserConfigDict
465+ | JSONSchemaParserConfigDict
466+ | OpenAPIParserConfigDict
467+ | GraphQLParserConfigDict ,
460468) -> _ConfigT :
461469 """Create a parser config from GenerateConfig with additional options.
462470
@@ -735,6 +743,28 @@ def get_header_and_first_line(csv_file: IO[str]) -> dict[str, Any]:
735743 ),
736744 }
737745
746+ # Convert schema_version string to appropriate enum based on input type
747+ jsonschema_version : JsonSchemaVersion | None = None
748+ openapi_version : OpenAPIVersion | None = None
749+ if config .schema_version and config .schema_version != "auto" :
750+ if input_file_type == InputFileType .OpenAPI :
751+ try :
752+ openapi_version = OpenAPIVersion (config .schema_version )
753+ except ValueError :
754+ valid = [v .value for v in OpenAPIVersion ]
755+ msg = f"Invalid OpenAPI version: { config .schema_version } . Valid values: { valid } "
756+ raise Error (msg ) from None
757+ elif input_file_type == InputFileType .GraphQL :
758+ msg = f"--schema-version is not supported for { input_file_type .value } "
759+ raise Error (msg )
760+ else :
761+ try :
762+ jsonschema_version = JsonSchemaVersion (config .schema_version )
763+ except ValueError :
764+ valid = [v .value for v in JsonSchemaVersion ]
765+ msg = f"Invalid JSON Schema version: { config .schema_version } . Valid values: { valid } "
766+ raise Error (msg ) from None
767+
738768 if input_file_type == InputFileType .OpenAPI :
739769 from datamodel_code_generator .parser .openapi import OpenAPIParser # noqa: PLC0415
740770
@@ -743,6 +773,7 @@ def get_header_and_first_line(csv_file: IO[str]) -> dict[str, Any]:
743773 "include_path_parameters" : config .include_path_parameters ,
744774 "use_status_code_in_response_name" : config .use_status_code_in_response_name ,
745775 "openapi_include_paths" : config .openapi_include_paths ,
776+ "openapi_version" : openapi_version ,
746777 ** additional_options ,
747778 }
748779 parser_config = _create_parser_config (OpenAPIParserConfig , config , openapi_additional_options )
@@ -760,7 +791,11 @@ def get_header_and_first_line(csv_file: IO[str]) -> dict[str, Any]:
760791 else :
761792 from datamodel_code_generator .parser .jsonschema import JsonSchemaParser # noqa: PLC0415
762793
763- parser_config = _create_parser_config (JSONSchemaParserConfig , config , additional_options )
794+ jsonschema_additional_options : JSONSchemaParserConfigDict = {
795+ "jsonschema_version" : jsonschema_version ,
796+ ** additional_options ,
797+ }
798+ parser_config = _create_parser_config (JSONSchemaParserConfig , config , jsonschema_additional_options )
764799 parser = JsonSchemaParser (source = source , config = parser_config ) # ty: ignore
765800
766801 with chdir (config .output ):
0 commit comments