Skip to content

[Java][JAX-RS] Add JSON-B discriminator deserialization support - #25663

Open
dean505 wants to merge 2 commits into
OpenAPITools:masterfrom
dean505:jaxrs-spec-jsonb-deserializers
Open

dean505 wants to merge 2 commits into
OpenAPITools:masterfrom
dean505:jaxrs-spec-jsonb-deserializers

Conversation

@dean505

@dean505 dean505 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds JSON-B discriminator-based polymorphic deserialization support to the Java JAX-RS Spec server generator.

Depends on #25662. This is a follow-up to the abstract models and sealed discriminator hierarchies PR. Please review and merge #25662 first.

Until #25662 is merged, this PR includes both commits in the GitHub comparison. The JSON-B-specific changes are contained in commit 88ef700f952.

Changes

  • Add the jsonbDiscriminatorDeserializers generator option.
  • Generate JSON-B deserializers for discriminator-based polymorphic model hierarchies.
  • Support nested polymorphic values in lists, maps, and sets.
  • Support @JsonbCreator, inherited models, oneOf interfaces, and discriminator mappings.
  • Add the JsonbPolymorphismProcessor and corresponding Mustache templates.
  • Add generator tests and OpenAPI specifications covering polymorphic JSON-B deserialization.
  • Add a generated JAX-RS Spec JSON-B sample and include it in the sample CI workflow.
  • Update generator documentation.

Validation

  • ./mvnw clean package — BUILD SUCCESS (all 8 modules; Maven Central used via a separate settings file).
  • Targeted JAX-RS Spec tests — 239 tests, 0 failures, 0 errors, 1 skipped.
  • ./bin/generate-samples.sh bin/configs/jaxrs-spec-sealed-jsonb.yaml — successful, no generated changes.
  • ./bin/utils/export_docs_generators.sh — completed, no generated changes.

PR checklist

  • Read the contribution guidelines.
  • Run Maven build and tests.
  • Regenerate the relevant JAX-RS Spec sample.
  • Export generator documentation.
  • Commit all changes.

Dependency

PR #25662 introduces the abstract and sealed model support used by this implementation. After #25662 is merged, this PR should contain only the JSON-B-specific changes relative to master.


Summary by cubic

Adds JSON-B discriminator-based polymorphic deserialization to the JAX-RS Spec server generator. Previously, discriminator hierarchies deserialized as their declared type; now the new jsonbDiscriminatorDeserializers option generates deserializers that resolve the concrete class from the discriminator value. This builds on the abstract models and sealed hierarchy support and should be reviewed and merged after that PR.

New Features

  • Generates JSON-B deserializers for discriminator-based hierarchies, including oneOf interfaces, inherited models, @JsonbCreator, and discriminator mappings.
  • Resolves polymorphic values nested in lists, sets, and maps, with container deserializers where the JSON-B implementation needs them.
  • Adds generator tests, test specifications, a generated sample with CI coverage, and updated generator docs.
  • With Yasson before 3.0.5, root containers such as Map<String, Pet> (also List<Pet> on 3.0.2) are not covered; Johnzon 2.0.2 to 2.2.0 has a missing-required-container limitation.

Written for commit 88ef700. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 52 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecCompileSupport.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecCompileSupport.java:101">
P2: `compileModels` can fail unguarded tests on the project's supported JDK 11 because `javac --release 17` is unavailable there. Guard this helper with `requireJdk(17)` (or pass a supported release) so every caller skips cleanly on older JDKs.</violation>
</file>

<file name="modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/jsonb_polymorphism.yaml">

<violation number="1" location="modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/jsonb_polymorphism.yaml:20">
P2: `Car` declares `carType` as its discriminator but does not require that property. OpenAPI 3.0 requires discriminator properties to be required, so schema validators and consumers can reject this fixture; add `carType` to the inline schema’s `required` list.</violation>
</file>

<file name="samples/server/petstore/jaxrs-spec-sealed-jsonb/README.md">

<violation number="1" location="samples/server/petstore/jaxrs-spec-sealed-jsonb/README.md:17">
P2: This run command targets TomEE 7, but the sample requires Java 17 and Jakarta EE APIs. Use a Jakarta-compatible TomEE 10 embedded plugin version, or remove the launch command.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

}
}
Path classes = output.resolve("classes");
CompileResult result = compile(sources, classes, "17", classpath);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: compileModels can fail unguarded tests on the project's supported JDK 11 because javac --release 17 is unavailable there. Guard this helper with requireJdk(17) (or pass a supported release) so every caller skips cleanly on older JDKs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecCompileSupport.java, line 101:

<comment>`compileModels` can fail unguarded tests on the project's supported JDK 11 because `javac --release 17` is unavailable there. Guard this helper with `requireJdk(17)` (or pass a supported release) so every caller skips cleanly on older JDKs.</comment>

<file context>
@@ -0,0 +1,165 @@
+            }
+        }
+        Path classes = output.resolve("classes");
+        CompileResult result = compile(sources, classes, "17", classpath);
+        assertEquals(result.exitCode, 0, result.diagnostics);
+        return classes;
</file context>
Suggested change
CompileResult result = compile(sources, classes, "17", classpath);
requireJdk(17);
CompileResult result = compile(sources, classes, "17", classpath);

required:
- petType
- name
properties:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Car declares carType as its discriminator but does not require that property. OpenAPI 3.0 requires discriminator properties to be required, so schema validators and consumers can reject this fixture; add carType to the inline schema’s required list.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/jsonb_polymorphism.yaml, line 20:

<comment>`Car` declares `carType` as its discriminator but does not require that property. OpenAPI 3.0 requires discriminator properties to be required, so schema validators and consumers can reject this fixture; add `carType` to the inline schema’s `required` list.</comment>

<file context>
@@ -0,0 +1,142 @@
+      required:
+        - petType
+        - name
+      properties:
+        petType:
+          type: string
</file context>

Please execute the following:

```
mvn -Dtomee-embedded-plugin.http=8080 package org.apache.tomee.maven:tomee-embedded-maven-plugin:7.0.5:run

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This run command targets TomEE 7, but the sample requires Java 17 and Jakarta EE APIs. Use a Jakarta-compatible TomEE 10 embedded plugin version, or remove the launch command.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At samples/server/petstore/jaxrs-spec-sealed-jsonb/README.md, line 17:

<comment>This run command targets TomEE 7, but the sample requires Java 17 and Jakarta EE APIs. Use a Jakarta-compatible TomEE 10 embedded plugin version, or remove the launch command.</comment>

<file context>
@@ -0,0 +1,27 @@
+Please execute the following:
+
+```
+mvn -Dtomee-embedded-plugin.http=8080 package org.apache.tomee.maven:tomee-embedded-maven-plugin:7.0.5:run
+```
+
</file context>
Suggested change
mvn -Dtomee-embedded-plugin.http=8080 package org.apache.tomee.maven:tomee-embedded-maven-plugin:7.0.5:run
mvn -Dtomee-embedded-plugin.http=8080 package org.apache.tomee.maven:tomee-embedded-maven-plugin:10.0.0:run

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant