How to use OpenAPI oneOf property with openapi-generator-maven-plugin when generating Spring code
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
oneOf is the OpenAPI tool for describing payloads that may take one of several distinct shapes. When you generate Spring code from such a schema, the generator can only do a good job if the models are clearly separated and the API description tells the deserializer how to choose the correct subtype.
Model oneOf as explicit component schemas
The most reliable approach is to define each variant as its own named schema under components/schemas, then reference those schemas from a parent object with oneOf. In practice, this is much easier for openapi-generator to handle than embedding anonymous schemas inline.
The key point is that the payload must validate against exactly one schema. If two variants overlap too much, generated code becomes awkward and runtime deserialization may fail.
In this example, the type field acts as a discriminator. That gives the generator and Jackson a concrete hint about which Java type should be used.
Configure the Maven plugin for Spring generation
The Maven plugin only reads what is in the OpenAPI document, so the spec matters more than plugin magic. Still, the generator needs a sensible Spring configuration so it emits interfaces, models, and Jackson annotations that fit a typical Spring Boot application.
You do not normally need a special flag just to "turn on" oneOf. The feature comes from the schema. If the output is poor, the root cause is usually an ambiguous OpenAPI definition rather than a missing plugin option.
What the generated Spring models look like
With a discriminator-based oneOf, the generated code usually creates a parent model or interface and subtype models for each branch. Jackson annotations are then used to deserialize incoming JSON into the correct subtype.
For example, a controller implementation might receive a generated base type and branch safely:
That works cleanly because the variants are structurally distinct and the JSON contains a discriminator value. Without that discriminator, the generated deserializer may have to guess based on fields, which is brittle.
Test the contract, not just the generated code
After generation, test the serialized JSON that your API accepts and returns. A valid schema can still behave badly at runtime if two branches are hard to distinguish or if your Spring configuration overrides Jackson defaults.
An integration test keeps the contract honest:
This kind of test is especially useful after changing the spec or upgrading the generator version.
Common Pitfalls
The most common mistake is using oneOf for schemas that are not truly exclusive. If one payload can satisfy two branches, validation and deserialization become unpredictable. Make the variants distinct by required fields or a discriminator.
Another issue is defining branch schemas inline instead of as named components. Code generation is usually clearer when each subtype has its own reusable schema.
Teams also forget that generator output depends on runtime libraries. If Jackson cannot resolve polymorphic types in your application, inspect the generated annotations and verify that your JSON actually includes the discriminator property.
Finally, do not assume every generator version behaves identically. If you upgrade the plugin, regenerate code and rerun contract tests before merging.
Summary
- Use
oneOfonly when the payload must match exactly one schema. - Prefer named component schemas instead of inline anonymous branches.
- Add a discriminator property so generated Spring models deserialize predictably.
- Treat the OpenAPI document as the primary fix when generated code looks wrong.
- Verify generated polymorphic models with integration tests against real JSON.
Related reading
- How to use python ray for independent computers (each have its username and password) via internet(distributed computation with ip address)?
- How to use ReadDirectoryChangesW method with completion routine?
- How to use RestSharp with async/await
- How to use SCNetworkReachability in Swift
- How to use Project Reactor's Scheduler with Executor based libraries?
- How to use projections and specifications with spring data jpa?
- How to use Session Affinity on requests to Kubernetes service?
- How to use SSH to run a local shell script on a remote machine?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.