Fix inline Map response examples being treated as media types

Merged
MA
9d ago
+53-2

Closes #25236.

An inline schema.example is example data, even when it is a Java Map. Route it through the same serialization path as other explicit examples instead of interpreting its keys as content types. The public generate(Map, ..., Schema/String) overloads still accept examples keyed by media type.

The explicit-example helper now checks the supplied example rather than the model registry, so inline examples also work when the public constructor receives a null registry.

Regression tests cover normal and empty maps, a data key named application/json, empty/null model registries, JSON/wildcard content types and response metadata. A separate test preserves the media-type map API.

Validation on JDK 21 with the Maven wrapper:

./mvnw -B -ntp -pl modules/openapi-generator -am \
  -Dtest=ExampleGeneratorTest,ModelUtilsTest,DefaultCodegenTest,AbstractJavaCodegenExampleValuesTest,StaticHtmlGeneratorTest \
  -Dsurefire.failIfNoSpecifiedTests=false -Djacoco.skip=true test
# 288 tests, 0 failures, 0 errors, 0 skipped

The three initial Map regressions fail on the unmodified implementation. Three additional null-registry cases fail with the first routing fix alone and pass with the corrected helper guard. I have not run the full generator suite or regenerated every sample.

PR checklist

  • Read the contribution guidelines.
  • Run the full project build, all sample generators and documentation export.
  • Mention language technical committee members, if applicable. This change is in the shared example generator.

I used Codex to help investigate the issue and prepare the implementation and regression tests.


Summary by cubic

Fixes inline Map response examples being misinterpreted as media-type mappings, so schema.example data now serializes correctly instead of having its keys treated as content types.

The public generate(Map<String, Object>, ...) overload retains its media-type-keyed behavior. Also fixes a null-guard bug so inline examples work when the model registry is null; regression tests cover normal and empty maps, application/json data keys, null registries, and the preserved media-type API.