Fred
Fred

Reputation: 781

Swagger/OpenApi - How to exclude property from equals/hascode methods

I want to exclude some properties from the equals/hascode methods when generating code via Swagger/OpenApi.

Here is an example of an object defined inside the YAML file :

ExampleDTO:
  type: object
  properties:
    id:
      type: integer
      format: int64
    property2:
      maxLength: 3
      minLength: 1
      pattern: '[0-9]*'
      type: string
    property3:
      maxLength: 5
      minLength: 1
      pattern: '[0-9]*'
      type: string

Here's the generated equals method :

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ExampleDTO exampleDTO = (ExampleDTO) o;
    return Objects.equals(this.id, ExampleDTO.id) &&
        Objects.equals(this.property2, exampleDTO.property2) &&
        Objects.equals(this.property3, exampleDTO.property3);
  }

The problem is that I only want the id property to be used in the equals method. How can I do that? Thanks!

Upvotes: 1

Views: 2453

Answers (1)

pringi
pringi

Reputation: 4652

Assuming that you are using "JavaSpring" generator, you can copy the templates from https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/JavaSpring to a local directory and modify, for example, https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/JavaSpring/pojo.mustache.

The equals method generation in the template looks like this:

 @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }{{#hasVars}}
    {{classname}} {{classVarName}} = ({{classname}}) o;
    return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
        {{/hasMore}}{{/vars}}{{#parent}} &&
        super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
    return super.equals(o);{{/hasVars}}
  }

Then when running "swagger-codegen-maven-plugin" you can pass the templateDirectory property to indicate where are located the templates.

<templateDirectory>myTemplateDir</templateDirectory>

As it is stated in the plugin homepage:

Modifying the client library format

Don't like the default swagger client syntax? Want a different language supported? No problem! Swagger Codegen processes mustache templates with the jmustache engine. You can modify our templates or make your own.

You can look at modules/swagger-codegen/src/main/resources/${your-language} for examples. To make your own templates, create your own files and use the -t flag to specify your template folder. It actually is that easy.

The problem with this approach is that will affect all "Pojo" objects.

If you want a more fine grained generation, then you will need to customize your own generator like extending an existing one (like JavaSpring) and specify it in

<language>com.my.package.for.GeneratorLanguage</language>

like it is informed here.

In this case you will need to change the templates so they can handle your specific processing for creating the class.

Upvotes: 3

Related Questions