swagger-api / swagger-api/swagger-codegen

[Java] swagger-codegen-maven-plugin auto-generated equals method does not call super.equals() for inheritance with no additional fields

Open
#12,152 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

In our project we use swagger-codegen-maven-plugin to generate java from yaml definitions. I have recently noticed problem with equals/hashCode method generation for auto-generated clases that inherits from other class but doesn't add any additional fields.

I have following yaml definition:

  A:
    type: object
    required:
      - fieldA1
      - fieldA2
    properties:
      fieldA1:
        type: string
      fieldA2:
        type: string
  B:
    allOf:
      - $ref: "#/definitions/A"
      - type: object

It results with equals method of B class being generated as follows:

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    return true;
  }

The problem is that auto-generated equals method in B does not call A.equals() - so comparision of two objects of B class always returns true no matter of value of fieldA1, fieldA2.

I would expect B.equals method to look like this:

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    return super.equals(o);

This has become a serious issue for our project. I would appreciate your support.
Thank you!

Swagger-codegen version

3.0.42

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue through swagger-codegen-maven-plugin using the YAML definitions for A and B, then trace the Java generation templates or entry points that produce B.equals. Done means generated subclasses with no additional fields delegate equality to their superclass, while the existing inheritance example still generates valid Java.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.