OpenAPI.NET
OpenAPI.NET copied to clipboard
V2 path item body parameters are not converted into requestBodies
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.
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.
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.
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.
I think same problem occurs with V3 as well. Use this attachment as input and see behaviour. petstore3_0_json.txt
@Dipak-Tated Writing out the $ref is by design.
@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
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.