OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.91k stars 6.58k forks source link

[BUG] 5.0 produces invalid Elm code with missing decoders/encoders for custom types #8343

Open SiriusStarr opened 3 years ago

SiriusStarr commented 3 years ago

In the petstore example, it looks like custom types's don't have their encoders/decoders properly written:

orderDecoder : Json.Decode.Decoder Order_
orderDecoder =
    Json.Decode.succeed Order_
        |> maybeDecode "id" Json.Decode.int Nothing
        |> maybeDecode "petId" Json.Decode.int Nothing
        |> maybeDecode "quantity" Json.Decode.int Nothing
        |> maybeDecode "shipDate" Api.Time.dateTimeDecoder Nothing
        |> maybeDecode "status" <!!orderStatusDecoder should be here!!> Nothing
        |> maybeDecode "complete" Json.Decode.bool (Just False)
encodeOrderPairs : Order_ -> List EncodedField
encodeOrderPairs model =
    let
        pairs =
            [ maybeEncode "id" Json.Encode.int model.id
            , maybeEncode "petId" Json.Encode.int model.petId
            , maybeEncode "quantity" Json.Encode.int model.quantity
            , maybeEncode "shipDate" Api.Time.encodeDateTime model.shipDate
            , maybeEncode "status" <!!encodeOrderStatus should be here!!> model.status
            , maybeEncode "complete" Json.Encode.bool model.complete
            ]
    in
    pairs

Additionally, there is an erroneous DEcoder in the middle of one of the ENcoders. fixed in master

This is the full diff between the output of 04dfff8 and a manually corrected version:

diff -r elm-openapi-test/src/Api/Data.elm elm-openapi-test-fix/src/Api/Data.elm
202c202
<             , maybeEncode "status"  model.status
---
>             , maybeEncode "status" encodeOrderStatus model.status
246c246
<             , maybeEncode "status"  model.status
---
>             , maybeEncode "status" encodePetStatus model.status
343c343
<         |> maybeDecode "status"  Nothing
---
>         |> maybeDecode "status" orderStatusDecoder Nothing
376c376
<         |> maybeDecode "status"  Nothing
---
>         |> maybeDecode "status" petStatusDecoder Nothing

Originally posted by @SiriusStarr in https://github.com/OpenAPITools/openapi-generator/issues/8218#issuecomment-747686514

SiriusStarr commented 3 years ago

Just pulled and confirmed that the issue with custom types persists in the latest master, 04dfff83e0717e693afb61499c62440491c1ac9c. The issue with the erroneous decoder appears to have been fixed, however, so I have struckout that above.