OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

V2 path item body parameters are not converted into requestBodies

Open darrelmiller opened this issue 6 years ago • 8 comments

darrelmiller avatar Jun 10 '18 14:06 darrelmiller

Is this issue referring to $ref variables in body parameters and responses not getting dereferenced for v2? I have been trying different releases with no luck. Specifically, I can see them being resolved in the OpenApiDocument model after a read, but then the write produces a JSON with references.

shametim avatar Jul 12 '18 18:07 shametim

The default behavior is to export referenced components as $ref. Can you show me what you are trying to achieve? I think I can create a walker very easily that will inline all $refs in a document.

darrelmiller avatar Jul 12 '18 22:07 darrelmiller

Thanks for the prompt reply.

Actual output of using the writer.

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Pet Store"
  },
  "paths": {
    "/api/pets": {
      "get": {
        "tags": [
          "animal"
        ],
        "operationId": "GetAllPets",
        "consumes": [
          "application/json",
        ],
        "produces": [
          "application/json",
        ],
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "required": false,
            "schema": {
              "$ref": "#/definitions/PetDetailsRequest"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "$ref": "#/definitions/PetDetailsResponse"
            }
          },
          "400": {
            "description": "Bad Request",
            "schema": {
              "$ref": "#/definitions/InvalidResponse"
            }
          }
        }
      }
    }
  }, 
"definitions": {
    "PetDetailsRequest": {
      "required": [
        "name"
      ],
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "age": {
          "type": "int"
        }
      },
    "PetDetailsResponse":  { ... }}}

Desired behavior:

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Pet Store"
  },
  "paths": {
    "/api/pets": {
      "get": {
        "tags": [
          "animal"
        ],
        "operationId": "GetAllPets",
        "consumes": [
          "application/json",
        ],
        "produces": [
          "application/json",
        ],
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "required": false,
            "schema": {    <---- populated by PetDetailsRequest definition
               "required": [
                   "name"
                 ],
             "type": "object",
             "properties": {
              "name": {
                 "type": "string"
               },
              "age": {
                 "type": "int"
              }
            },
             "example":{
                "name": "Toby",
                "age": 3,
             },
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "$ref": "#/definitions/PetDetailsResponse"  <--- this would be replaced by PetDetailsResponse definition (not included for brevity)
            }
          },
          "400": {
            "description": "Bad Request",
            "schema": {
              "$ref": "#/definitions/InvalidResponse" <-- same
            }
          }
        }
      }
    }
  }, 
"definitions": {
    "PetDetailsRequest": {
      "required": [
        "name"
      ],
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "age": {
          "type": "int"
        }
      },
      "example":{
         "name": "Toby",
         "age": 3,
      },
    "PetDetailsResponse":  { ... }}} etc..

This is useful because it makes the swagger much more accessible to a consumer.

shametim avatar Jul 12 '18 23:07 shametim

Ok. It's not a common request to do this because most tooling that presents OpenAPI docs to users would generally do the inlining on the fly, rather than updating the document. However, you could easily achieve this by creating a visitor that clears the Reference property from any object that has it set. Once the Reference property is clear the writer will write out the full object.

You would create a new visitor class that derives from the VisitorBase, override the Visit Schema method and set the Reference property to null. Then use the Walker to walk the Document using the visitor and then pass the document to the writer.

If I get a chance while doing some updates over the next few days I can create a sample.

darrelmiller avatar Jul 13 '18 16:07 darrelmiller

I think same problem occurs with V3 as well. Use this attachment as input and see behaviour. petstore3_0_json.txt

Dipak-Tated avatar Oct 19 '18 10:10 Dipak-Tated

@Dipak-Tated Writing out the $ref is by design.

darrelmiller avatar Oct 20 '18 01:10 darrelmiller

@darrelmiller Is there a sample code to dereference the references in the swagger document. We have a requirement where we have to flatten all the members in the response, we are unable to do so using OpenAPI sdk

pratap-bhaskar avatar Mar 07 '19 11:03 pratap-bhaskar

This solved my requirement

public class NullReferenceVisitor : OpenApiVisitorBase
    {
        public override void Visit(OpenApiSchema schema)
        {
            schema.Reference = null;
        }
    }

Passing this as referencevisitor instance while parsing the document worked for me, i was able to serialize all the properties of the response.

pratap-bhaskar avatar Apr 02 '19 06:04 pratap-bhaskar