Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum generation ignores existingJavaType #1182

Closed
jhhdk opened this issue Dec 2, 2020 · 11 comments
Closed

Enum generation ignores existingJavaType #1182

jhhdk opened this issue Dec 2, 2020 · 11 comments
Milestone

Comments

@jhhdk
Copy link

jhhdk commented Dec 2, 2020

When running jsonSchema2Pojo it seems it generates enums regardless of whether I specified to use existingJavaType, that seems a bit odd.
If I wanted to generatePojo's that used an enum i wrote myself, it feels like i should be able to.

@unkish
Copy link
Collaborator

unkish commented Dec 2, 2020

Would it be possible to provide some sample schema(s) ?

@jhhdk
Copy link
Author

jhhdk commented Dec 2, 2020 via email

@unkish
Copy link
Collaborator

unkish commented Dec 2, 2020

I'm sorry but I don't see any schemas attached 😕

@jhhdk
Copy link
Author

jhhdk commented Dec 2, 2020 via email

@jhhdk
Copy link
Author

jhhdk commented Dec 3, 2020

test.zip
Here are the aforementioned files.

@unkish
Copy link
Collaborator

unkish commented Dec 3, 2020

If I wanted to generatePojo's that used an enum i wrote myself, it feels like i should be able to.

If enum would be omitted from property definition:

    "properties": {
        "bar": {
            "existingJavaType": "com.examples.types.NonGeneratedBar",
            "type": "string",
            "enum": [
                "BAZ",
                "BAR"
            ]
        }
    }

then generated POJO would be referencing given type eg.:

    "properties": {
        "bar": {
            "existingJavaType": "com.examples.types.NonGeneratedBar"
        }
    }

Considering that the referenced existing java type could be:

  1. non enum type, in which case enum definition would be misleading
  2. enum but with diferent set of constants or equivalent set of constants with different ordinal values, in which case enum definition would be misleading
  3. enum that either could have been generated via same definition in separate file or non-generated having same values, in which case it would be data duplication.

At this point I fail to understand what is the use-case for having both existingJavaType and enum defined in such a way?

@jhhdk
Copy link
Author

jhhdk commented Dec 7, 2020 via email

@unkish
Copy link
Collaborator

unkish commented Dec 7, 2020

Hi

Enums are classes that can have any number of properties and methods.

I do believe that this is only partially correct. Enums are special types which may have any number of properties and methods.


If I want my enum to have methods that I define, I want to be able to tell jsonschema2Pojo to just let me handle those enums

This could be achieved by using existingJavaType (note that it's not accompanied by enum definition) eg:

    "properties": {
        "bar": {
            "existingJavaType": "com.examples.types.NonGeneratedEnum"
        }
    }

I still want to use json validation for that enum and i still want other classes to be generated.

I'm sorry but I don't understand what kind of JSON validation for enum is expected to happen (the only one I could think of is defined by required)

@joelittlejohn
Copy link
Owner

I think the point here is that you still want to include the enum rule in your schema, because when the schema is used for validation you want the enum values defined and applied. It's good for documentation too, of course.

It should be pretty simple to ignore enum when existingJavaType is defined.

@jhhdk
Copy link
Author

jhhdk commented Dec 7, 2020 via email

@unkish
Copy link
Collaborator

unkish commented Dec 7, 2020

Until PR it should be possible to workaround by supplying custom RuleFactory with either getSchemaRule or getEnumRule being overridden such that in case when there are both enum and existingJavaType present latter would take precedence.
Eg. custom EnumRule with:

    @Override
    public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container, Schema schema) {
        if (node.has("existingJavaType")) {
            return ruleFactory.getTypeRule().apply(nodeName, node, parent, container.getPackage(), schema);
        }
        return super.apply(nodeName, node, parent, container, schema);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants