OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java][Spring] Missing examples parameter in @Schema annotation for OAS 3.1 spec

Open
#24,755 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If an API spec compliant with OAS 3.1 contains multiple examples for a property (using the examples keyword), the examples parameter is not generated for the corresponding @Schema annotation in Java.

openapi-generator version

v7.25.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Multiple object examples.
  version: 0.0.1
paths:
  /api/example:
    get:
      responses:
        "200":
          description: Response schema with multiple examples.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExampleResponse'
components:
  schemas:
    ExampleResponse:
      properties:
        test:
          type: string
          examples: [ 'test string 1', 'test string 2' ]
Generation Details
java -jar openapi-generator-cli-7.25.0.jar generate \
  --generator-name spring \
  --model-package example \
  --input-spec openapi.yaml
Steps to reproduce

Generated src/main/java/example/ExampleResponse.java file contains @Schema annotation for getTest() getter method without examples parameter:

@Schema(name = "test", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("test")
public @Nullable String getTest() {
    return test;
}

while it should be:

@Schema(name = "test", examples = { "test string 1", "test string 2" }, requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("test")
public @Nullable String getTest() {
    return test;
}
Related issues/PRs
Suggest a fix

Add public List<String> examples; to CodegenProperty class and implement related handling, such as parsing examples array.

Update pojo.mustache with generation of examples for @Schema annotation with values from CodegenProperty.examples.

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 by reading the CodegenProperty class and the Spring generator's pojo.mustache template, then run the supplied OAS 3.1 specification with the documented spring generation command. Trace how property examples are parsed and rendered. Done means the generated ExampleResponse.java getter includes both example values in its @Schema annotation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.