[java][jaxrs-spec] - Add support for sealed classes and interfaces with useSealed flag

Merged
IG
4mo ago
+1095-3

Summary

Adds an opt-in useSealed option to the jaxrs-spec generator (follows #24174). When enabled, generated models form closed (sealed) hierarchies using Java 17 sealed types, mirroring the existing useSealed semantics of the Spring generator. The generated pom.xml targets Java 17 when the option is on (default stays 1.8), and the default output without the flag is unchanged (guarded by a regression test).

Scenarios covered

Spec shapeFlagsGenerated code
oneOf schema + discriminatoruseOneOfInterfaces + useSealedpublic sealed interface PetRequest permits CatRequest, DogRequest — subtypes are final classes implementing it
allOf hierarchy (parent holds the discriminator)useSealed onlypublic sealed class Pet permits Cat, Dog — subclasses are final and extends Pet
Multi-level hierarchy (middle tier with its own discriminator)useSealed onlypublic sealed class Cat extends Pet permits PersianCat — sealed and permitted at the same time; leaves are final
oneOf schema + discriminatoruseSealed without useOneOfInterfacesWithout useOneOfInterfaces a oneOf schema is generated as a regular class with the members' properties merged in (unchanged from master). Its members are unrelated classes — they do not extend it — so sealing it over them would not compile. The generator therefore emits it as a plain final class: internally, postProcessAllModels removes the oneOf-derived permits entries that core populates unconditionally
Standalone model (no subtypes)useSealedpublic final class Toy
Any specflag off (default)Output unchanged — no sealed/final/permits, pom stays at Java 1.8

Each scenario is pinned by a test in JavaJAXRSSpecServerCodegenTest, and a jaxrs-spec-sealed sample (registered in the JDK17 samples workflow) compiles the sealed output in CI. A full 771-config sample + docs regeneration produces zero diffs outside this PR's own sample and doc rows.


PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds useSealed to jaxrs-spec to generate sealed oneOf interfaces and sealed allOf parent classes with final subtypes. Also switches generated POMs to Java 17 when enabled, adds a jaxrs-spec-sealed sample to the JDK 17 workflow, and updates docs.

  • New Features

    • With useOneOfInterfaces=true, the oneOf interface is sealed and permits its subtypes; subtypes are final and implement it.
    • For discriminator-driven allOf hierarchies, the parent class is sealed and permits its subclasses; subclasses are final; standalone models become final.
    • POM uses Java 17 when useSealed is on; adds jaxrs-spec-sealed sample and registers it in the JDK 17 samples workflow; docs list the option in jaxrs-spec and jaxrs-cxf-cdi.
  • Bug Fixes

    • When useSealed is enabled without useOneOfInterfaces, drop oneOf-derived permits from plain classes to avoid compile errors.