Skip to content

[elm] Fix decoding empty operation responses #5055

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

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions modules/openapi-generator/src/main/resources/elm/Api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ send toMsg (Request req) =
, headers = req.headers
, url = Url.Builder.crossOrigin req.basePath req.pathParams req.queryParams
, body = req.body
, expect = Http.expectJson toMsg req.decoder
, expect = expectJson toMsg req.decoder
, timeout = req.timeout
, tracker = req.tracker
}
Expand Down Expand Up @@ -118,4 +118,45 @@ interpolatePath rawPath pathParams =

queries : List (String, Maybe String) -> List Url.Builder.QueryParameter
queries =
List.filterMap (\(key, value) -> Maybe.map (Url.Builder.string key) value)
List.filterMap (\(key, value) -> Maybe.map (Url.Builder.string key) value)


expectJson : (Result Http.Error a -> msg) -> Json.Decode.Decoder a -> Http.Expect msg
expectJson toMsg decoder =
Http.expectStringResponse toMsg <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)

Http.Timeout_ ->
Err Http.Timeout

Http.NetworkError_ ->
Err Http.NetworkError

Http.BadStatus_ metadata _ ->
Err (Http.BadStatus metadata.statusCode)

Http.GoodStatus_ _ body ->
if String.isEmpty body then
-- we might 'expect' no body if the return type is `()`
case Json.Decode.decodeString decoder "{}" of
Ok value ->
Ok value

Err _ ->
decodeBody decoder body

else
decodeBody decoder body


decodeBody : Json.Decode.Decoder a -> String -> Result Http.Error a
decodeBody decoder body =
case Json.Decode.decodeString decoder body of
Ok value ->
Ok value

Err err ->
Err (Http.BadBody (Json.Decode.errorToString err))
45 changes: 43 additions & 2 deletions samples/openapi3/client/elm/src/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ send toMsg (Request req) =
, headers = req.headers
, url = Url.Builder.crossOrigin req.basePath req.pathParams req.queryParams
, body = req.body
, expect = Http.expectJson toMsg req.decoder
, expect = expectJson toMsg req.decoder
, timeout = req.timeout
, tracker = req.tracker
}
Expand Down Expand Up @@ -118,4 +118,45 @@ interpolatePath rawPath pathParams =

queries : List (String, Maybe String) -> List Url.Builder.QueryParameter
queries =
List.filterMap (\(key, value) -> Maybe.map (Url.Builder.string key) value)
List.filterMap (\(key, value) -> Maybe.map (Url.Builder.string key) value)


expectJson : (Result Http.Error a -> msg) -> Json.Decode.Decoder a -> Http.Expect msg
expectJson toMsg decoder =
Http.expectStringResponse toMsg <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)

Http.Timeout_ ->
Err Http.Timeout

Http.NetworkError_ ->
Err Http.NetworkError

Http.BadStatus_ metadata _ ->
Err (Http.BadStatus metadata.statusCode)

Http.GoodStatus_ _ body ->
if String.isEmpty body then
-- we might 'expect' no body if the return type is `()`
case Json.Decode.decodeString decoder "{}" of
Ok value ->
Ok value

Err _ ->
decodeBody decoder body

else
decodeBody decoder body


decodeBody : Json.Decode.Decoder a -> String -> Result Http.Error a
decodeBody decoder body =
case Json.Decode.decodeString decoder body of
Ok value ->
Ok value

Err err ->
Err (Http.BadBody (Json.Decode.errorToString err))