swagger-api / swagger-api/swagger-codegen

[Java] Add an option to not generate equals/hashcode/toString

Open
#12,248 0 comments 1 reaction 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

The pojo.mustache template used for Java models always includes equals, hashcode and toString methods. There may be occurrences when they are not relevant, or we don't want them generated (the toString might reveal confidential information if the model is logged, the equals on all fields might not be relevant…).

Swagger-codegen version

3.0.47, not a regression as far as I can tell.

Command line used for generation

(using maven plugin)

Steps to reproduce

Generate for language java, spring, or logically any java-derived language.

Related issues/PRs

I couldn't find any.

Suggest a fix/enhancement

The simplest way to circumvent this would be to add an option not to generate those methods, or two separate options, maybe:

  • generateToString (default: true)
  • generateEqualsAndHashCode (default: true)

I suppose it could be handled in AbstractJavaCodegen.java + pojo.mustache.

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

Start with AbstractJavaCodegen.java and the pojo.mustache template, then run Java or Spring generation through the Maven plugin to observe the current output. Add configurable behavior for generating toString and equals/hashCode, preserving the default behavior, and verify that disabling the options removes the corresponding methods from generated models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.