[BUG][ELIXIR] deserialisation only happens when response has status 200
Bug Report Checklist
- [X] Have you provided a full/minimal spec to reproduce the issue?
- [X] Have you validated the input using an OpenAPI validator (example)?
- [X] What's the version of OpenAPI Generator used?
- [X] Have you search for related issues/PRs?
- [X] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix (example)
Description
Based on the sigature of RequestBuilder.decode, a deserialisation only happens when the statuscode is 200 - no mather of what's defined in the OpenApi Spec
openapi-generator version
- v3.3.4
- v4.0.0-beta2
OpenAPI declaration file content
openapi: 3.0.0
info:
title: Random Title
version: 1.0.0
servers:
- url: https://api.example.com/
paths:
/create:
post:
operationId: createSomething
responses:
201:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Something'
components:
schemas:
Something:
type: object
properties:
id:
type: string
example: OK
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/openapi.yaml \
-g elixir \
-o /local/out
Steps to reproduce
Based on the following line, a propper match on the 201 statuscode would be imposible.
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache#L124
the function createSomething will always return the tesla.env instead of the propper deserialized Something Model
Related issues/PRs
none
Suggest a fix
Add a switch case to the createSomething pipe sequence (in this example...) to determinate the expected module based on the response statuscode, default should be matched with {:ok, %Tesla.Env{}} ->
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
👍
FWIW, I faced this issue before and solved it in the following way in RequestBuilder:
I added this macro:
defmacro is_success(code) do
quote do: unquote(code) in [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]
end
and updated the decode/1 logic as follows:
@spec decode(Tesla.Env.t | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: code, body: body}) when is_success(code), do: Poison.decode(body)
def decode(response), do: {:error, response}
@spec decode(Tesla.Env.t | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: code} = env, false) when is_success(code), do: {:ok, env}
def decode(%Tesla.Env{status: code, body: body}, struct) when is_success(code), do: Poison.decode(body, as: struct)
def decode(response, _struct), do: {:error, response}