Skip to content

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

@juanmanuelromeraferrio

Description

@juanmanuelromeraferrio

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions