3

I am uncertain what to search for to solve this issue, and/or what the name is of the domain of the problem.

I am attempting to digest an API using swagger.

Consider this response:

"responses": {
    "200": {
        "description": "OK",
        "schema": {
            "type": "array",
            "items": {
                "$ref": "#/components/schemas/Pet/cat"
            }
        }
    }
}

With the typical cat definition from the examples:

  "cat" : {
    "summary": "An example of a cat",
    "value": 
      {
        "name": "Fluffy",
        "petType": "Cat",
        "color": "White",
        "gender": "male",
        "breed": "Persian"
      }
  }

My question arises from the fact that the documentation for the API has the caveat that all responses are wrapped, despite the swagger definition not indicating it.

This is from their documentation (minor edits):

  Responses
    HTTP responses with JSON format bodies. The structure of the response body is as follows:


    {
        "Ok": bool,
        "SomeReference": int,
        "Data": Entity,
        "ErrorMessage": string
    }


    Ok: determines if request was successful or not

    SomeReference: reference number or id that link an outside item to an Api action

    Data: If request was successful, this will contain an object representing the expected result (cat/dog/frog/etc). These object structures are explained below. Otherwise, Data is null

    ErrorMessage: If request was unsuccessful, an error message providing a reason for failure. Otherwise, Data is null

The gist of it is that while their swagger says they are going to return a cat, dog, etc, they actually return this other (non-defined) object, which itself contains a property that houses the actual response.

This is a sample response that would get returned from the API:

{
  "Ok":true,
  "SomeReference": 12345,
  "Data": {
        "name": "Fluffy",
        "petType": "Cat",
        "color": "White",
        "gender": "male",
        "breed": "Persian"
  },
  "ErrorMessage":""
}

I have tried looking for wrapper, envelope, etc but can't determine how to solve this in a deterministic fashion.

I am using NSwag to generate C# code, but searching for envelopes or wrappers yields unrelated results with respect to code generation and this type of difference between spec and reality.

Is the swagger definition incorrect, or am I misunderstanding a fundamental aspect of responses and/or how to deal with wrappers in every response (more likely)?

Jim Hume
  • 51
  • 1
  • 6

0 Answers0