Skip to content

JSON schema with enum member with a name starting with a capital letter, causes a generation of a code that doesn't compile #126

@yurama

Description

@yurama

When trying to run jsonschema2pojo on a json schema that has an enum property with a name starting with a capital letter, the generated code doesn't compile.
The compilation error is: non-static variable 'x' cannot be referenced from a static context.

That error occurs because the generated code has a private field with the same name as the enum, that is later accessed by that name from a static code block.

According to RFC 4627 - there is no requirement for member names to start with lower case letters.

For example, for the JSON schema:


{
    "type": "object",
    "properties": {
        "ExampleObj": {
            "type": "object",
            "properties": {
                "TimeFormat": {
                    "enum": [ "12h", "24h" ],
                    "description": "The referred time display format, either 12 or 24 hour clock."
                }
            }
        }       
    }
}

That error occurs because the generated code has a private field with the name TimeFormat:


    @JsonProperty("TimeFormat")
    private ExampleObj.TimeFormat TimeFormat;

and an enum with the same name, that tries to access that field from within a static code block (note the ExampleObj.TimeFormat.values() within the foreach loop):


    @Generated("com.googlecode.jsonschema2pojo")
    public static enum TimeFormat {

        _12_H("12h"),
        _24_H("24h");
        private final String value;
        private static Map constants = new HashMap();

        static {
            for (ExampleObj.TimeFormat c: ExampleObj.TimeFormat.values()) {
                constants.put(c.value, c);
            }
        }
.
.
.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions