Skip to content

[BUG] ExampleGenerator.generateFromResponseSchema treats the keys of an inline object example (java.util.Map) as media types #25236

Closed

Description

@knusth

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 generateFromResponseSchema(statusCode, schema, produces) gets an inline (non-$ref, non-array) schema whose example is a java.util.Map, the "primitive type" branch casts the example to Map<String, Object> and passes it as the examples argument of generate(Map, List, Schema). That argument means {mediaType -> example}, so every key of the example becomes a contentType and every value becomes a separate example:

Schema<Object> response = new ObjectSchema()
        .addProperty("id", new IntegerSchema())
        .addProperty("name", new StringSchema());
response.setExample(Map.of("id", 1, "name", "Rex"));

new ExampleGenerator(Map.of(), new OpenAPI())
        .generateFromResponseSchema("200", response, Set.of("application/json"));
// actual:   [{contentType=name, example="Rex", statusCode=200}, {contentType=id, example=1, statusCode=200}]
// expected: [{contentType=application/json, example={"id":1,"name":"Rex"}, statusCode=200}]

When the spec is read from a file, swagger-parser gives the example as a Jackson ObjectNode, not a Map, so the CLI does not hit this path. It affects programmatic use, such as custom generators or tools that build or transform the OpenAPI model with plain Java maps.

openapi-generator version

7.25.0 (also current master).

Suggest a fix

In generateFromResponseSchema(Schema, Set), handle any non-null example (including Map) through generate(Object, List), and use the generate(Map, ...) overloads only for the media-type map.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions