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

Consumes in global context is not propagated to the request body

Open PerthCharern opened this issue 6 years ago • 1 comments

consumes in global context is not propagated to the request body

We need to fix it in a similar way to how we fixed produces in #267 and #398.

{
    "swagger": "2.0",
    "info": {
        "title": "Two responses",
        "version": "1.0.0"
    },
    "schemes": [
        "https"
    ],
    "basePath": "/",
    "paths": {
        "/items": {
            "post": {
                "responses": {
                    "200": {
                        "description": "An OK response",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/Item"
                            }
                        }
                    },
                    "default": {
                        "description": "An error response",
                        "schema": {
                            "$ref": "#/definitions/Error"
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "name",
                        "in": "formData",
                        "description": "Updated name of the pet",
                        "required": false,
                        "type": "string"
                    }
                ]
            },
            "patch": {
                "responses": {
                    "200": {
                        "description": "An OK response",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/Item"
                            }
                        }
                    },
                    "default": {
                        "description": "An error response",
                        "schema": {
                            "$ref": "#/definitions/Error"
                        }
                    }
                },
                "produces": [
                    "application/json",
                    "application/xml"
                ],
                "consumes": [
                    "application/json",
                    "text/plain"
                ],
                "parameters": [
                    {
                        "name": "name",
                        "in": "formData",
                        "description": "Updated name of the pet",
                        "required": false,
                        "type": "string"
                    }
                ]
            }
        }
    },
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "definitions": {
        "Item": {
            "properties": {
                "id": {
                    "type": "string",
                    "description": "Item identifier."
                }
            }
        },
        "Error": {
            "properties": {
                "code": {
                    "type": "integer",
                    "format": "int32"
                },
                "message": {
                    "type": "string"
                },
                "fields": {
                    "type": "string"
                }
            }
        }
    }
}

With the above document, the media type added to POST does not respect the "application/json" defined in the global level.

PerthCharern avatar Apr 17 '19 00:04 PerthCharern

As this problem only occurs when consumes is declared after paths/components I am going to defer fixing this. The fix needs to go into v2\OpenApiDocumentDeserializer.

darrelmiller avatar Mar 15 '21 01:03 darrelmiller

Closing this as done as per this snippet: https://github.com/microsoft/OpenAPI.NET/blob/d812ec5d97bbe9f24a8363c28e9dd42b43eccfa9/src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs#L182-L202

MaggieKimani1 avatar Feb 12 '24 15:02 MaggieKimani1