swagger-api / swagger-api/swagger-codegen-generators
[Java] Issue with OneOf types
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
If we have an API definition like so:
components:
schemas:
Foo:
type: object
properties:
foo:
type: string
Bar:
type: object
properties:
bar:
type: string
Schema:
oneOf:
- $ref: '#/components/schemas/Foo'
- $ref: '#/components/schemas/Bar'
Swagger will generate an interface OneOfSchema , as well as 3 implementors: Schema, Foo, Bar.
The problem is that the APIs (and models) will refer to the empty stub class Schema, e.g.:
public Schema somethingGet(Schema body) throws ApiException {...}
The problem is you when you call this you get issues, e.g.:
var foo = new Foo();
...
api.somethingGet(foo); //doesn't compile because Foo is not a Schema.
The fix is to rename the interface to be Schema and only have 2 implementors: Foo, Bar.
I had a go at doing this myself, seems like a change to subclass SchemaHandler.processComposedSchema() for java so that it's like so:
@Override
protected CodegenModel processComposedSchema(CodegenModel codegenModel, ComposedSchema composedSchema, Map<String, CodegenModel> allModels) {
List<Schema> schemas = composedSchema.getOneOf();
//Give the composedModel the same name as the parent:
CodegenModel composedModel = this.createComposedModel(codegenModel.getName(), schemas);
if (composedModel == null) { //returns null if it's not OneOf
schemas = composedSchema.getAnyOf();
composedModel = this.createComposedModel(ANY_OF_PREFFIX + codegenModel.getName(), schemas);
if (composedModel == null) {
return null;
}
}
this.addInterfaceModel(codegenModel, composedModel);
this.addInterfaces(schemas, composedModel, allModels);
//This is a dirty way of saying we don't want to generate the original model
codegenModel.setClassname("X"+codegenModel.classname);
codegenModel.setName("X"+codegenModel.name);
return composedModel;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Java SchemaHandler.processComposedSchema() entry point and reproduce the supplied oneOf definition to inspect the generated interface, implementors, and API signature. Done means the generated API accepts Foo or Bar directly without the empty Schema stub being used as a separate model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100