Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[elm-client] Recursive type aliases #310

Closed
andys8 opened this issue Jun 13, 2018 · 1 comment
Closed

[elm-client] Recursive type aliases #310

andys8 opened this issue Jun 13, 2018 · 1 comment

Comments

@andys8
Copy link
Contributor

andys8 commented Jun 13, 2018

A recursive data structure will lead to elm code, which is not being parsed.
The main issue and how to workaround is described here: https://github.com/elm/compiler/blob/0.18.0/hints/recursive-alias.md

    "tags": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
        },
        "children": {
          "type": "array",
          "items": { "$ref": "#/definitions/tags" }
        }
      }
    }

Will generate this:

type alias Tags =
    { id : UUID
    , children : List Tags
    }


tagsDecoder : Decoder Tags
tagsDecoder =
    decode Tags
        |> required "id" uUIDDecoder
        |> required "children" (Decode.list tagsDecoder)


tagsEncoder : Tags -> Encode.Value
tagsEncoder model =
    Encode.object
        [ ( "id", uUIDEncoder model.id )
        , ( "children", (Encode.list << List.map tagsEncoder) model.children )
        ]

But should be something like this, where Decode.lazy is used and a type definition for the recursive list:

type alias Tags =
    { id : UUID
    , children : Children
    }


type Children
    = Children (List Tags)


childrenToTags : Children -> List Tags
childrenToTags (Children tags) =
    tags


tagsDecoder : Decoder Tags
tagsDecoder =
    decode Tags
        |> required "id" uUIDDecoder
        |> required "children"
            (Decode.map Children (Decode.list (Decode.lazy (\_ -> tagsDecoder))))


tagsEncoder : Tags -> Encode.Value
tagsEncoder model =
    Encode.object
        [ ( "id", uUIDEncoder model.id )
        , ( "children", (Encode.list << List.map tagsEncoder) (childrenToTags model.children) )
        ]

OpenAPI spec version: 0.4.0
Generator version 3.0.1

@eriktim
Copy link
Contributor

eriktim commented Jan 20, 2020

This is now part of the upcoming 5.0.0 release.

@eriktim eriktim closed this as completed Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants