Open
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
JavaInflector/pojo.mustache does not generate proper hashCode and equals functions
openapi-generator version
latest
OpenAPI declaration file content or url
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person/display/{personId}:
get:
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Person"
components:
schemas:
Person:
type: object
discriminator:
propertyName: $_type
mapping:
a: '#/components/schemas/Adult'
c: Child
properties:
$_type:
type: string
lastName:
type: string
firstName:
type: string
Adult:
description: A representation of an adult
allOf:
- $ref: '#/components/schemas/Person'
- type: object
properties:
children:
type: array
items:
$ref: "#/components/schemas/Child"
Child:
description: A representation of a child
allOf:
- type: object
properties:
age:
type: integer
format: int32
- $ref: '#/components/schemas/Person'
Command line used for generation
docker run --rm -v /local/openapi:/local openapitools/openapi-generator-cli generate -o /local/out -i /local/allOf.yaml -g java-inflector
Steps to reproduce
in the Generated Classes you can see that the super class is not considered in equals or hash code
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Adult adult = (Adult) o;
return Objects.equals(children, adult.children);
}
@Override
public int hashCode() {
return Objects.hash(children);
}
After the patch below
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Adult adult = (Adult) o;
return Objects.equals(this.children, adult.children) &&
super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(children, super.hashCode());
}
Related issues/PRs
Suggest a fix
diff --git a/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
index badfdbc35a..f824eb3af9 100644
--- a/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
+++ b/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
@@ -48,7 +48,6 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
}
{{/vars}}
-
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
@@ -57,15 +56,17 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
if (o == null || getClass() != o.getClass()) {
return false;
}
- {{classname}} {{classVarName}} = ({{classname}}) o;{{#hasVars}}
- return {{#vars}}Objects.equals({{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
- {{/hasMore}}{{^hasMore}};{{/hasMore}}{{/vars}}{{/hasVars}}{{^hasVars}}
+ {{#hasVars}}
+ {{classname}} {{classVarName}} = ({{classname}}) o;
+ return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
+ {{/hasMore}}{{/vars}}{{#parent}} &&
+ super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
return true;{{/hasVars}}
}
@Override
public int hashCode() {
- return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}});
+ return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
}
@Override