Skip to content

Rust-reqwest client fails because of null values in tags arrays from petstore.swagger.io #2003

Closed
@sverch

Description

@sverch

Steps to Reproduce

1. Generate the rust-reqwest based client

docker run --rm -v ${PWD}:/local/out/rust/ openapitools/openapi-generator-cli generate     -i http://petstore.swagger.io/v2/swagger.json     -g rust     -o /local/out/rust/generated --additional-properties packageName=petstore_client --library=reqwest

2. Use it to call find_pets_by_status against petstore.swagger.io

use petstore_client;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let configuration = petstore_client::apis::configuration::Configuration::new();
    let apiclient = petstore_client::apis::client::APIClient::new(configuration);
    let status = vec![std::string::String::from("pending")];
    let result = apiclient.pet_api().find_pets_by_status(status);
    println!("result = {:?}", result);
    Ok(())
}

3. See error with null tags

result = Err(Reqwest(Error(Json(Error("invalid type: null, expected struct Tag", line: 1, column: 2807)))))

Background

I generated the rust-reqwest based client and tried to do a find_pets_by_status operation, but I got this error:

result = Err(Reqwest(Error(Json(Error("invalid type: null, expected struct Tag", line: 1, column: 2807)))))

Sure enough, when I hit the API directly I see that there are null values in the tags arrays for some pets:

$ curl -X GET "https://petstore.swagger.io/v2/pet/findByStatus?status=pending" -H  "accept: application/json" | jq ".[].tags"
...
[
  {
    "id": 0,
    "name": "string"
  }
]
[
  null
]
[
  null
]
[
  null
]
[
  {
    "id": 0,
    "name": "Asdasd"
  }
]
...

The spec that I generated this client from doesn't say anything about nulls being allowed:

$ curl --silent --output - http://petstore.swagger.io/v2/swagger.json | jq ".definitions.Pet.properties.tags"
{
  "type": "array",
  "xml": {
    "name": "tag",
    "wrapped": true
  },
  "items": {
    "$ref": "#/definitions/Tag"
  }
}

The spec doesn't mention null at all:

$ curl --silent --output - http://petstore.swagger.io/v2/swagger.json | jq "." | grep null
(nothing)

I'm not sure if nulls should be allowed in arrays and the client is wrong, or if they shouldn't and the server is wrong. I see this issue which makes me think null values being allowed wasn't intended to be the default.

To get it working, I had to change this line in the Pet model:

    pub tags: Option<Vec<crate::models::Tag>>,

To this:

    pub tags: Option<Vec<Option<crate::models::Tag>>>,

Thanks for your work on this project! Let me know if there's a better place I should file this.

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