Fixed HttpResponse type is generated even when multiple valid response types are defined.
Ciwi opened this issue · 0 comments
Ciwi commented
Setup
When defining an endpoint
/multiresponse/:
parameters:
- $ref: '#/components/parameters/some_parameter'
get:
operationId: getMultiResponse
responses:
200:
$ref: '#/components/responses/VariantAVO'
300:
$ref: '#/components/responses/VariantBVO'
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
403:
$ref: '#/components/responses/NotAllowed'
404:
$ref: '#/components/responses/NotFound'
the generated sources for the API will be defined like this:
@io.micronaut.http.annotation.Get("/multiresponse/")
@io.micronaut.http.annotation.Produces("application/json")
io.micronaut.http.HttpResponse<VariantAVO> getMultiResponse(
...
even if the endpoint could also return VariantBVO
.
When trying to retrieve VariantBVO
from a response like this:
Optional<VariantBVO> optionalVariant = response.getBody(VariantBVO.class);
optionalVariant
is empty
event when HTTP status code 300
was returned.
When removing the defined type from the generated method like this:
io.micronaut.http.HttpResponse getMultiResponse(
the response correctly contains an instance of VariantBVO
.
Versions
micronaut => 1.3.7
micronaut-openapi-codegen => 0.11.0
Suggested solution
Remove the generic type for HttpResponse
from generated API when an endpoint could return multiple (different) response types.