Skip to content

InvalidResponsePayload instead of Option::None for nullable api endpoint. #1197

@mhuttner

Description

@mhuttner

I have an API endpoint that may return no response or some type Inode like this:

 "/fs/resolve/{path}": {
      "get": {
        "operationId": "resolvePath",
        "responses": {
          "2XX": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "allOf": [
                    {
                      "$ref": "#/components/schemas/Inode"
                    }
                  ],
                  "nullable": true
                }
              }
            }
          }
        },
        "tags": [
          "Filesystem"
        ],
        "parameters": [
          {
            "in": "path",
            "name": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },

typify correctly generates an Option type but if the API returns an empty body i get an InvalidResponsePayload error.
I tracked this down to serde being unable to parse an empty byte array b"" to an Option None here:

    pub async fn from_response<E>(response: reqwest::Response) -> Result<Self, Error<E>> {
        let status = response.status();
        let headers = response.headers().clone();
        let full = response.bytes().await.map_err(Error::ResponseBodyError)?;
        let inner =
            serde_json::from_slice(&full).map_err(|e| Error::InvalidResponsePayload(full, e))?;

        Ok(Self {
            inner,
            status,
            headers,
        })
    }

Can a case for this be added?
I already tried adding a specific 204 Response to the schema, but this fails with assertion failed: response_types.len() <= 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions