diff --git a/jsonschema_gentypes/__init__.py b/jsonschema_gentypes/__init__.py index 7a007adc..2dd26b78 100644 --- a/jsonschema_gentypes/__init__.py +++ b/jsonschema_gentypes/__init__.py @@ -673,6 +673,7 @@ def get_name( ], proposed_name: Optional[str] = None, upper: bool = False, + get_name_properties: Optional[str] = None, ) -> str: """ Get the name for an element. @@ -694,6 +695,11 @@ def get_name( name = name.upper() # Remove spaces return prefix + "".join(["_" if char.isspace() else char for char in name]) + elif get_name_properties == "UpperFirst": + # Change just the first letter to upper case + name = name[0].upper() + name[1:] + # Remove spaces + return prefix + "".join([char for char in name if not char.isspace()]) else: # Title case name = name.title() diff --git a/jsonschema_gentypes/api.py b/jsonschema_gentypes/api.py index 04cebb2f..2ab6080c 100644 --- a/jsonschema_gentypes/api.py +++ b/jsonschema_gentypes/api.py @@ -53,12 +53,14 @@ def __init__( self, resolver: RefResolver, additional_properties: configuration.AdditionalProperties = configuration.ADDITIONALPROPERTIES_ONLY_EXPLICIT, + get_name_properties: configuration.GetNameProperties = configuration.GETNAMEPROPERTIES_TITLE, ) -> None: """ Initialize with a resolver. """ self.resolver = resolver self.additional_properties = additional_properties + self.get_name_properties = get_name_properties # types by reference self.ref_type: dict[str, Type] = {} self.root: Optional[TypeProxy] = None @@ -170,13 +172,15 @@ def get_type( if description: if not isinstance(the_type, NamedType): if auto_alias: - the_type = TypeAlias(get_name(schema_meta_data, proposed_name), the_type, description) + the_type = TypeAlias( + self.get_name(schema_meta_data, proposed_name), the_type, description + ) the_type.set_comments(description) if "default" in schema_meta_data: the_type.add_depends_on( Constant( - f"{get_name(schema_meta_data, proposed_name, True)}_DEFAULT", + f"{self.get_name(schema_meta_data, proposed_name, True)}_DEFAULT", schema_meta_data["default"], [f"Default value of the field path '{proposed_name}'"], ) @@ -191,6 +195,19 @@ def get_type( return the_type + def get_name( + self, + schema: Optional[ + Union[ + jsonschema_draft_04.JSONSchemaD4, + jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019, + ] + ], + proposed_name: Optional[str] = None, + upper: bool = False, + ) -> str: + return get_name(schema, proposed_name, upper, self.get_name_properties) + def resolve_ref( self, schema: Union[ @@ -342,7 +359,7 @@ def build_type( "anyof", ) if not isinstance(type_, NamedType): - type_ = TypeAlias(get_name(schema_meta_data, proposed_name), type_) + type_ = TypeAlias(self.get_name(schema_meta_data, proposed_name), type_) elif type_.comments(): type_.comments().append("") type_.comments().append("Aggregation type: anyOf") @@ -394,7 +411,7 @@ def build_type( if len(schema_type) == 0: return BuiltinType("None") inner_types = [] - name = get_name(schema_meta_data, proposed_name) + name = self.get_name(schema_meta_data, proposed_name) has_title = "title" in schema_meta_data proposed_name = schema_meta_data.get("title", proposed_name) diff --git a/jsonschema_gentypes/api_draft_04.py b/jsonschema_gentypes/api_draft_04.py index 576a86d9..fa1355d9 100644 --- a/jsonschema_gentypes/api_draft_04.py +++ b/jsonschema_gentypes/api_draft_04.py @@ -55,7 +55,7 @@ def enum( ) return TypeEnum( - get_name(schema_meta_data, proposed_name), + self.get_name(schema_meta_data, proposed_name), cast(list[Union[int, float, bool, str, None]], schema["enum"]), get_description(schema_meta_data), ) @@ -99,7 +99,7 @@ def object( ) std_dict = None - name = get_name(schema_meta_data, proposed_name) + name = self.get_name(schema_meta_data, proposed_name) schema.setdefault("used", set()).add("additionalProperties") # type: ignore[typeddict-item] additional_properties = cast( Union[jsonschema_draft_04.JSONSchemaD4, jsonschema_draft_2020_12_applicator.JSONSchemaD2020], @@ -294,7 +294,9 @@ def any_of( ) if not isinstance(type_, NamedType): type_ = TypeAlias( - get_name(combined_schema_meta_data, proposed_name + " " + sub_name), type_, [] + self.get_name(combined_schema_meta_data, proposed_name + " " + sub_name), + type_, + [], ) additional_types.append(type_) @@ -326,7 +328,9 @@ def any_of( ) if not isinstance(type_, NamedType): type_ = TypeAlias( - get_name(combined_schema_meta_data, proposed_name + " " + sub_name), type_, [] + self.get_name(combined_schema_meta_data, proposed_name + " " + sub_name), + type_, + [], ) additional_types.append(type_) inner_types.append(type_) @@ -495,7 +499,7 @@ def all_of( ) if not isinstance(type_, NamedType): type_ = TypeAlias( - get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"), + self.get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"), type_, [], ) @@ -522,7 +526,7 @@ def all_of( ) if not isinstance(type_, NamedType): type_ = TypeAlias( - get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"), + self.get_name(combined_schema_meta_data, f"{proposed_name} {sub_name}{index}"), type_, [], ) diff --git a/jsonschema_gentypes/configuration.py b/jsonschema_gentypes/configuration.py index 161a9c73..e202b2c3 100644 --- a/jsonschema_gentypes/configuration.py +++ b/jsonschema_gentypes/configuration.py @@ -32,6 +32,13 @@ class ApiArguments(TypedDict, total=False): Describe how to deal with additional properties """ + get_name_properties: "GetNameProperties" + """ + Get name properties. + + Describe the rules to use to get the name of an element + """ + class Configuration(TypedDict, total=False): """ @@ -101,6 +108,18 @@ class GenerateItem(TypedDict, total=False): """ +GetNameProperties = Union[Literal["Title"], Literal["UpperFirst"]] +""" +Get name properties. + +Describe the rules to use to get the name of an element +""" +GETNAMEPROPERTIES_TITLE: Literal["Title"] = "Title" +"""The values for the 'Get name properties' enum""" +GETNAMEPROPERTIES_UPPERFIRST: Literal["UpperFirst"] = "UpperFirst" +"""The values for the 'Get name properties' enum""" + + PRE_COMMIT_ARGUMENTS_DEFAULT: list[Any] = [] """ Default value of the field path 'Pre-commit configuration arguments' """ diff --git a/jsonschema_gentypes/schema.json b/jsonschema_gentypes/schema.json index fe89d105..c64067b0 100644 --- a/jsonschema_gentypes/schema.json +++ b/jsonschema_gentypes/schema.json @@ -60,6 +60,12 @@ "title": "Additional properties", "description": "Describe how to deal with additional properties", "enum": ["Always", "Only explicit"] + }, + "get_name_properties": { + "type": "string", + "title": "Get name properties", + "description": "Describe the rules to use to get the name of an element", + "enum": ["Title", "UpperFirst"] } } }, diff --git a/tests/get_name_properties.json b/tests/get_name_properties.json new file mode 100644 index 00000000..cc9df7bf --- /dev/null +++ b/tests/get_name_properties.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "definitions": { + "SubresourceUris": { + "type": "object", + "properties": { + "feedback": { + "type": "string" + } + }, + "required": ["feedback"], + "title": "SubresourceUris" + } + }, + "type": "object", + "properties": { + "subresource_uris": { + "$ref": "#/definitions/SubresourceUris" + } + }, + "required": ["subresource_uris"], + "title": "ResponseType" +} diff --git a/tests/get_name_properties.py b/tests/get_name_properties.py new file mode 100644 index 00000000..c8ba70d9 --- /dev/null +++ b/tests/get_name_properties.py @@ -0,0 +1,21 @@ +from typing import TypedDict + +from typing_extensions import Required + + +class ResponseType(TypedDict, total=False): + """ResponseType.""" + + subresource_uris: Required["SubresourceUris"] + """ + SubresourceUris. + + Required property + """ + + +class SubresourceUris(TypedDict, total=False): + """SubresourceUris.""" + + feedback: Required[str] + """ Required property """ diff --git a/tests/test_get_name_properties.py b/tests/test_get_name_properties.py new file mode 100644 index 00000000..88841839 --- /dev/null +++ b/tests/test_get_name_properties.py @@ -0,0 +1,22 @@ +from jsonschema_gentypes.cli import process_config +from jsonschema_gentypes.configuration import Configuration + + +def test_empty_array() -> None: + config: Configuration = Configuration( + generate=[ + { + "source": "tests/get_name_properties.json", + "destination": "tests/get_name_properties.py", + "api_arguments": {"get_name_properties": "UpperFirst"}, + } + ], + ) + process_config( + config, + ["tests/get_name_properties.json"], + ) + + with open("tests/get_name_properties.py") as f: + content = f.read() + assert "class SubresourceUris" in content