Search code examples
swaggeropenapiswagger-2.0

Can we set global "consumes" and "produces" in OpenAPI/Swagger 2.0?


In the each path I need to set consumes and produces. Can I set them globally?

post:
      summary: ""
      description: ""
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"

Solution

  • Sure. You can specify consumes and produces on the root level of the spec, and they will be inherited by all operations. Global consumes and produces can be overridden on the operation level if needed.

    swagger: '2.0'
    ...
    
    consumes:
      - application/json
      - application/xml
    produces:
      - application/xml
      - application/json
    
    paths:
      /foo:
        get:
          # This inherits global `produces`
          ...
    
        post:
          # Here we override global `consumes`
          consumes:
            - application/x-www-form-urlencoded
          ...
        
    

    More info: https://swagger.io/docs/specification/2-0/mime-types/