Skip to content

Consumes in global context is not propagated to the request body #400

Closed

Description

@PerthCharern

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.

Activity

  1. added this to the Backlog milestone on Apr 26, 2020
  2. darrelmiller commented on Mar 15, 2021

    @darrelmiller
    Member

    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.

  3. modified the milestones: 1.3, on Mar 15, 2021
  4. modified the milestones: , 2.0 on Feb 10, 2022
  5. MaggieKimani1 commented on Feb 12, 2024

    @MaggieKimani1
    Contributor

    Closing this as done as per this snippet:

    internal static OpenApiRequestBody CreateRequestBody(
    ParsingContext context,
    OpenApiParameter bodyParameter)
    {
    var consumes = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationConsumes) ??
    context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalConsumes) ??
    new List<string> { "application/json" };
    var requestBody = new OpenApiRequestBody
    {
    Description = bodyParameter.Description,
    Required = bodyParameter.Required,
    Content = consumes.ToDictionary(
    k => k,
    _ => new OpenApiMediaType
    {
    Schema = bodyParameter.Schema,
    Examples = bodyParameter.Examples
    }),
    Extensions = bodyParameter.Extensions
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugA broken experience

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions