joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Exclude additionalProperties from hashCode and equals #847

Open jleelong opened 6 years ago

jleelong commented 6 years ago

Hi @joelittlejohn,

Great tool, thanks for providing it to the community! Quick question, I would like to exclude additionalProperties from the generated hashCode and equals methods. I'm also using a custom schema definition for additionalProperties, not sure if that makes a difference? I've tried to exclude the additionalProperties below but it doesn't appear to give the expected result.

Schema Definition

...
    "type": "object",
    "additionalProperties": {
        "type": "array",
        "excludedFromEqualsAndHashCode": true,
        "minItems": 1,
        "items": {
            "type": "object",
            "excludedFromEqualsAndHashCode": true,
            "additionalProperties": true
        }
    },
    "properties": {
...

Source Generator I'm also using the Java API to read the schemas and generate source files

    private void generateClass(String schema, String resourcePath,
            String outputPath, String destinationPackage) throws IOException {
        JCodeModel codeModel = new JCodeModel();
        URL source = new File(resourcePath.concat(schema)).toURI().toURL();

        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() {
                return true;
            }

            @Override
            public boolean isIncludeAdditionalProperties() {
                return true;
            }

            @Override
            public boolean isFormatDates() {
                return true;
            }

            @Override
            public boolean isFormatTimes() {
                return true;
            }

            @Override
            public boolean isFormatDateTimes() {
                return true;
            }
        };

        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config,
                new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
        mapper.generate(codeModel, schema.replace(".json", ""), destinationPackage, source);
        codeModel.build(new File(outputPath));
    }

Generated Methods Unfortunately the additionalProperties are still added to the generated methods

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof BitemporalDataProperty) == false) {
            return false;
        }
        BitemporalDataProperty rhs = ((BitemporalDataProperty) other);
        return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).isEquals();
    }

I'm using version 0.5.1 with jdk 1.8.0_121. Let me know if you need any other information and thanks in advance for your help.

Kind Regards, Lee Long

jleelong commented 6 years ago

Hi @joelittlejohn, do you have any recommendations for above. We're using additionalProperties to store transaction details on bitemporal data, but we can't use that information for comparing two objects.

Thanks!

joelittlejohn commented 6 years ago

I think it would be possible for us to support excludedFromEqualsAndHashCode in the additionalProperties schema, but the feature was never intended to work there. The additional properties java field is effectively a meta field that represents many possible JSON fields, whereas the excludedFromEqualsAndHashCode extension property was intended to allow excluding specific JSON fields from these methods.

I think it would be reasonable to implement this though, you could probably just do an additional check on the additionalProperties node here:

https://github.com/joelittlejohn/jsonschema2pojo/blob/c4adf1366c3d1b3aede7f0927bf85dd9b85b7e38/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ObjectRule.java#L579

Feel free to submit a PR.

sush77 commented 5 years ago

isIncludeAdditionalProperties() should return false to exclude additional properties.