[elm-client] Recursive type aliases
Created by: andys8
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