OpenAPITools / OpenAPITools/openapi-generator

deafultValue not added to POJO schema in spring-boot generator

Open
#16,998 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When using the spring generator, the @Schema annotation is missing the "defaultValue" parameter for properties that define a default in the openapi YAML file.

openapi-generator version

7.1.0

OpenAPI declaration file content or url
openapi: '3.0.3'
info:
  title: blah blah blan
  version: '1.0'
components:
  schemas:
    Options:
      type: object
      properties:
        xxx:
          type: boolean
          default: false
          description: this is xxx

Generates:

@Schema(name = "xxx", description = "this is xxx", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("xxx")
  public Boolean getXxx() {
    return xxx;
  }

Should generate:

@Schema(name = "xxx", description = "this is xxx", requiredMode = Schema.RequiredMode.NOT_REQUIRED, defaultValue = "false")
  @JsonProperty("xxx")
  public Boolean getXxx() {
    return xxx;
  }
Command line used for generation

Using maven plugin, but can be reproduced with:

java -jar ~/git/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i openapi.yaml

Suggest a fix/enhancement

I think the resources/JavaSpring/pojo.mustache file needs something like this:
Current:

{{#swagger2AnnotationLibrary}}
  @Schema(name = "{{{baseName}}}"{{#isReadOnly}}, accessMode = Schema.AccessMode.READ_ONLY{{/isReadOnly}}{{#example}}, example = "{{{.}}}"{{/example}}{{#description}}, description = "{{{.}}}"{{/description}}{{#deprecated}}, deprecated = true{{/deprecated}}, requiredMode = {{#required}}Schema.RequiredMode.REQUIRED{{/required}}{{^required}}Schema.RequiredMode.NOT_REQUIRED{{/required}})
  {{/swagger2AnnotationLibrary}}

New:

{{#swagger2AnnotationLibrary}}
  @Schema(name = "{{{baseName}}}"{{#isReadOnly}}, accessMode = Schema.AccessMode.READ_ONLY{{/isReadOnly}}{{#example}}, example = "{{{.}}}"{{/example}}{{#description}}, description = "{{{.}}}"{{/description}}{{#deprecated}}, deprecated = true{{/deprecated}}, requiredMode = {{#required}}Schema.RequiredMode.REQUIRED{{/required}}{{^required}}Schema.RequiredMode.NOT_REQUIRED{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}})
  {{/swagger2AnnotationLibrary}}

This same change likely needs to be done anywhere the @Schema is generated for swagger2.

When I tried this locally, it worked for simple properties, but for objects, it added 'defaultValue = "new ArrayList<>()"' instead.

Since I was not sure what other implications this might have, I did not want to suggest via a PR. I'll leave that to someone with more experience.

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 with the supplied OpenAPI YAML and generation command, then inspect resources/JavaSpring/pojo.mustache and the other swagger2 @Schema generation locations. Compare simple and object properties with defaults; done means generated annotations include the correct defaultValue without using generated object expressions such as new ArrayList<>().

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
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.