From 5b5df5b86306567f4824a4ba12f6fb2715a0bde6 Mon Sep 17 00:00:00 2001 From: Semen Date: Mon, 25 May 2020 11:56:11 +0300 Subject: [PATCH] feat: add generation of equals and hashCode to the model (#49) --- README.md | 1 + package.json | 5 +++++ .../java/com/asyncapi/model/$$message$$.java | 19 +++++++++++++++++++ .../java/com/asyncapi/model/$$schema$$.java | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 84156e822..a79ca31d4 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ components: |Name|Description|Required|Default| |---|---|---|---| +|disableEqualsHashCode|Disable generation of equals and hashCode methods for model classes.|No|`false`| |inverseOperations|Generate an application that will publish messages to `publish` operation of channels and read messages from `subscribe` operation of channels. Literally this flag will simply swap `publish` and `subscribe` operations in the channels.
This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.|No|`false`| |listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.|No|`3000`| |listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.|No|`3`| diff --git a/package.json b/package.json index 74c0956f9..d02d89324 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,11 @@ "default": false, "required": false }, + "disableEqualsHashCode": { + "description": "Disable generation of equals and hashCode methods for model classes.", + "default": "false", + "required": false + }, "listenerPollTimeout": { "description": "Only for Kafka. Timeout to use when polling the consumer.", "default": 3000, diff --git a/template/src/main/java/com/asyncapi/model/$$message$$.java b/template/src/main/java/com/asyncapi/model/$$message$$.java index 64de57d3b..0391ec003 100644 --- a/template/src/main/java/com/asyncapi/model/$$message$$.java +++ b/template/src/main/java/com/asyncapi/model/$$message$$.java @@ -1,5 +1,7 @@ package {{ params['userJavaPackage'] }}.model; +import java.util.Objects; + {% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %} * {{ line | safe}}{% endfor %}{% if message.examples() %} * Examples: {{message.examples() | examplesToString | safe}}{% endif %} @@ -16,6 +18,23 @@ public void setPayload({{payloadName}} payload) { this.payload = payload; } + {% if params.disableEqualsHashCode === 'false' %}@Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + {{messageName | camelCase | upperFirst}} event = ({{messageName | camelCase | upperFirst}}) o; + return Objects.equals(this.payload, event.payload); + } + + @Override + public int hashCode() { + return Objects.hash(payload); + }{% endif %} + @Override public String toString() { return "class {{messageName | camelCase | upperFirst}} {\n" + diff --git a/template/src/main/java/com/asyncapi/model/$$schema$$.java b/template/src/main/java/com/asyncapi/model/$$schema$$.java index ae96fc25c..5bfb79027 100644 --- a/template/src/main/java/com/asyncapi/model/$$schema$$.java +++ b/template/src/main/java/com/asyncapi/model/$$schema$$.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; +import java.util.Arrays; import java.util.Objects; {% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %} @@ -109,6 +110,23 @@ public String toString() { this.{{varName}} = {{varName}}; } {% endfor %} + {% if params.disableEqualsHashCode === 'false' %}@Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + {{schemaName | camelCase | upperFirst}} {{schemaName | camelCase}} = ({{schemaName | camelCase | upperFirst}}) o; + return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}{% if prop.type() === 'array' %}{% set varName = propName | camelCase + 'Array' %}{% endif %} + {% if prop.type() === 'array' %}Arrays{% else %}Objects{% endif %}.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %} + } + + @Override + public int hashCode() { + return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if prop.type() === 'array' %}Array{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}); + }{% endif %} @Override public String toString() {