OpenAPITools / OpenAPITools/openapi-generator
[REQ] Feature Request: Shared type between GET and POST methods instead of eg. GetFoo*Response and CreateFoo*Response
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
I'm using server-side JAXRS-SPEC generator (7.6.0)
Let's consider some example endpoint, involving a base class Foo with two children Bar and Baz:
...
"paths": {
...
"/foo/{id}": {
"get": {
"operationId": "getFoo",
...
"responses": {
"default": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Bar"
},
{
"$ref": "#/components/schemas/Baz"
}
],
"discriminator": {
"propertyName": "@type"
}
}
}
}
}
}
},
"post": {
"operationId": "createFoo",
...
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Bar"
},
{
"$ref": "#/components/schemas/Baz"
}
],
"discriminator": {
"propertyName": "@type"
}
}
}
}
},
...
Since the GET method might return an object either of type Bar or Baz, the generated return type named GetFooDefaultResponse contains all attributes of the parent and both (sub)type classes from which one can extract appropriate attributes for desired type.
Now, for the POST method, the requestBody is specified in a way that completely corresponds to the default response of the GET above, so it gets generated like this:
@Override
public CreateFooDefaultResponse createFoo(String id, GetFooDefaultResponse getFooDefaultResponse) {
return ....;
}
I find this problematic for several reasons:
- naming of the incoming argument type is counter-intuitive, it doesn't suggest that it is actually the
requestBody(according to my current level of knowledge, the motivation behind this is to re-use the type already given within theGETmethod?) - the return type of the
POSTis namedCreateFooDefaultResponseand is identical to theGetFooDefaultResponsein structure - due to the above fact, it's an obvious violation of the DRY principle which causes me big issues when converting between the types in my app's converter class.
Describe the solution you'd like
A solution to the problem might be some of the following:
- should the generator recognise that the types have identical structure (and in my case I can't imagine a scenario where they'd be different?) -- name them the same
- in my case the subtypes even have the
allOfwhich should be a strong indication to the generator that they inherit fromFoo
- in my case the subtypes even have the
- make these types somehow
castable/convertible between themselves (generate utility converter methods, or similar)
Describe alternatives you've considered
- a converter method in my app to convert between the types (likey in both ways!!)
- maybe get some hints here how I could overcome this should a solution already be there
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 by reproducing the server-side JAXRS-SPEC generator 7.6.0 output from the issue's OpenAPI example, focusing on the GET response and POST request body schemas. Done should address the duplicate GetFooDefaultResponse and CreateFooDefaultResponse structures and the counter-intuitive generated POST argument type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100