Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .generator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
format_data_with_schema,
get_response_type,
upperfirst,
escape_method_reserved_name,
)


Expand Down Expand Up @@ -75,6 +76,7 @@ def lookup(value, path):
JINJA_ENV.globals["format_parameters"] = format_parameters
JINJA_ENV.globals["get_response_type"] = get_response_type
JINJA_ENV.globals["get_type_at_path"] = openapi.get_type_at_path
JINJA_ENV.filters["escape_method_reserved_name"] = escape_method_reserved_name

JAVA_EXAMPLE_J2 = JINJA_ENV.get_template("example.j2")

Expand Down
1 change: 1 addition & 0 deletions .generator/src/generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def cli(specs, output):
env.filters["inline_docstring"] = formatter.inline_docstring
env.filters["un_parameterize_type"] = formatter.un_parameterize_type
env.filters["is_parameterized_type"] = formatter.is_parameterized_type
env.filters["escape_method_reserved_name"] = formatter.escape_method_reserved_name

env.globals["enumerate"] = enumerate
env.globals["get_name"] = openapi.get_name
Expand Down
13 changes: 13 additions & 0 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"do",
"else",
"enum",
"equals",
"extends",
"false",
"final",
Expand Down Expand Up @@ -74,6 +75,10 @@
"\\": "\\\\",
}

METHOD_KEYWORDS = {
"Class",
}


PATTERN_DOUBLE_UNDERSCORE = re.compile(r"__+")
PATTERN_LEADING_ALPHA = re.compile(r"(.)([A-Z][a-z0-9]+)")
Expand Down Expand Up @@ -125,6 +130,14 @@ def untitle_case(value):
def upperfirst(value):
return value[0].upper() + value[1:]

def escape_method_reserved_name(method_name):
"""
Escape reserved language keywords for method names like getClass, setClass, isClass, etc.
"""
if method_name in METHOD_KEYWORDS:
return f"{method_name}Attribute"
return method_name


def schema_name(schema):
if not schema:
Expand Down
4 changes: 2 additions & 2 deletions .generator/src/generator/templates/modelSimple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public class {{ name }} {%- if model.get("x-generate-alias-as-model") %} extends
{%- if schema.additionalProperties is defined and schema.additionalProperties is not false %}{%- if schema.additionalProperties.nullable %}content = JsonInclude.Include.ALWAYS,{%- endif %}{%- endif %}
value = JsonInclude.Include.{%- if isRequired %}ALWAYS{%- else %}USE_DEFAULTS{%- endif %})
{%- endif %}
public {{ dataType }} get{{ attr|camel_case|upperfirst }}() {
public {{ dataType }} get{{ attr|camel_case|upperfirst|escape_method_reserved_name }}() {
{%- if not isRequired and isNullable %}
{%- if schema.get("readOnly", False) %}

Expand Down Expand Up @@ -247,7 +247,7 @@ public class {{ name }} {%- if model.get("x-generate-alias-as-model") %} extends
{%- endif %}

{%- if not schema.get("readOnly", False) %}
public void set{{ attr|camel_case|upperfirst }}({{ dataType }} {{ variableName }}) {
public void set{{ attr|camel_case|upperfirst|escape_method_reserved_name }}({{ dataType }} {{ variableName }}) {
{%- if schema.enum is defined %}
if (!{{ variableName }}.isValid()) {
this.unparsed = true;
Expand Down
Loading