Skip to content

NoContent responses are parsed as JSON, and fail #302

@nikitaDanilenko

Description

@nikitaDanilenko

Describe the bug

  1. Have an OpenAPI specification with an endpoint returning NoContent
  2. Generate the request for this endpoint
  3. The request fails

To Reproduce

  1. Have this OpenAPI specification
     {
       "openapi": "3.0.0",
       "info": {
         "title": "NoContent issue",
         "version": "0.1"
       },
       "paths": {
         "/empty": {
           "post": {
             "responses": {
               "204": {
                 "description": "Returns nothing"
               }
             }
           }
         }
       }
     }
  2. Run the generator, and obtain
     empty config =
         Http.request
             { url = Url.Builder.absolute [ "empty" ] []
             , method = "POST"
             , headers = []
             , expect =
                 OpenApi.Common.expectJsonCustom
                     config.toMsg
                     (Dict.fromList [])
                     (Json.Decode.succeed ())
             , body = Http.emptyBody
             , timeout = Nothing
             , tracker = Nothing
             }
  3. Run the empty request
  4. Obtain a failure.

When matching on the failure type, the error is a OpenApi.Common.BadBody (also generated). The underlying reason is that the generated OpenApi.Common.expectJsonCustom does this in the good case:

    Http.GoodStatus_ httpMetadata body ->
        case Json.Decode.decodeString successDecoder body of

The issue is that for NoContent the call Json.Decode.decodeString successDecoder body tries to decode the case where body is the empty string. Since the empty string is not valid JSON, the decoding fails.

One possible solution is to check the httpMetadata.statusCode to specifically handle the 204 case differently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions