OpenAPITools / OpenAPITools/openapi-generator

[BUG][JavaSpring] requestBody named examples (OAS3) not exposed to Mustache template variables

Open
#23,607 1 comment 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?

Description

When defining multiple named examples on a requestBody following the OAS3 specification for adding examples, the examples variable on bodyParam is empty in the Mustache template context. This means it is impossible to generate proper @RequestBody Swagger annotations with examples using the JavaSpring generator, even when the spec is fully valid OAS3.

The data is clearly being parsed (it appears in {{jsonSchema}} as an HTML-encoded string), but it is never mapped into the examples field.


Openapi-generator version

7.21.0


OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Example API
  version: 1.0.0
paths:
  /users:
    post:
      summary: Adds a new user
      tags:
        - User
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/User"
            examples:
              Jessica:
                value:
                  id: 10
                  name: Jessica Smith
              Ron:
                value:
                  id: 11
                  name: Ron Stewart
      responses:
        "200":
          description: OK
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string

Generation Details
generatorName: spring
 dateLibrary: java8,
 hideGenerationTimestamp: true
 useBeanValidation: true
 useTags: true
 delegatePattern: true
 requestMappingMode: none
 useJakartaEe: true

Steps to reproduce
  1. Use the spec above with the spring generator
  2. Create a custom api.mustache with the following debug block added inside the method:
{{#bodyParam}}
/*
  DEBUG bodyParam dump:
  example: {{example}}
  examples: {{examples}}
  jsonSchema: {{jsonSchema}}
*/
{{/bodyParam}}
  1. Generate and inspect the output

Actual output:

example: new User()
examples:            <-- empty
jsonSchema: { "content" : { "application/json" : { "schema" : { "$ref" : "#/components/schemas/User" },
  "examples" : { "Jessica" : { "value" : { "id" : 10, "name" : "Jessica Smith" } },
  "Ron" : { "value" : { "id" : 11, "name" : "Ron Stewart" } } } } } }

Expected output:

{{examples}} should be populated and iterable, allowing the template to generate:

@io.swagger.v3.oas.annotations.parameters.RequestBody(
    content = @io.swagger.v3.oas.annotations.media.Content(
        mediaType = "application/json",
        schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = User.class),
        examples = {
            @io.swagger.v3.oas.annotations.media.ExampleObject(
                name = "Jessica",
                value = "{\"id\": 10, \"name\": \"Jessica Smith\"}"
            ),
            @io.swagger.v3.oas.annotations.media.ExampleObject(
                name = "Ron",
                value = "{\"id\": 11, \"name\": \"Ron Stewart\"}"
            )
        }
    )
)

Related issues/PRs
Suggest a fix

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 reproducing the issue with the provided OpenAPI declaration and custom api.mustache debug block, then trace how the JavaSpring generator builds the bodyParam template variables. Done means the named requestBody examples are populated and iterable as {{examples}}, enabling the shown annotation output.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.