Search code examples
swaggerswagger-2.0swagger-editor

Swagger: Additional properties not allowed: allOf


I'm trying to figure out this swagger API inheritance stuff by using allOf. This is my swagger yaml file.

swagger: '2.0'
info:
  title: Test API
  version: '1'
basePath: /api/v1
schemes:
  - https
produces:
  - application/json

paths:
  /users:
    get:
      summary: Collection of users
      tags:
        - users
      responses:
        200:
          description: A list of Users
          schema:
            $ref: "#/definitions/Users"        
        500:
          $ref: "#/responses/BadRequest"

definitions:
  User:
    required:
      - username
    properties:
      firstName:
        type: string
      lastName:
        type: string
      username:
        type: string
  Users:
    type: array
    items:
      $ref: "#/definitions/User"

responses:
  NonSuccess:
    description: Generic response for all non-success responses
    schema:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          description: The success code, 0 or -1.
        message:
          type: string
          description: The description message for this success code
        errors:
          type: array
          description: A map of errors within the request. Keyed by the parameter name and the values are the error details

  BadRequest:
    description: Invalid request parameters
    allOf:
      - $ref: "#/responses/NonSuccess"

When I paste this into the online editor, I get the following errors that I'm having a real hard time trying to figure out.

✖ Swagger Error
Additional properties not allowed: allOf
Jump to line 60

✖ Swagger Error
Not a valid response definition
Jump to line 22

The main problem seems to be Additional properties not allowed: allOf and I'm can't seem to figure out what I'm doing wrong in this case. I was trying to declare a basic non-success response so that all non-200 responses will inherit so that the API will have a very standard looking non-success response. I was under the impression I could do this with allOf and then add or overwrite the fields from that response. What exactly am I doing wrong?


Solution

  • The allOf tag can only be used on Schema objects. You can definitely use it on the Schema portion of the response, though. Here's an example of that.

    swagger: '2.0'
    info:
      title: Test API
      version: '1'
    basePath: /api/v1
    schemes:
      - https
    produces:
      - application/json
    
    paths:
      /users:
        get:
          summary: Collection of users
          tags:
            - users
          responses:
            200:
              description: A list of Users
              schema:
                $ref: "#/definitions/Users"        
            500:
              $ref: "#/responses/BadRequest"
    
    definitions:
      User:
        required:
          - username
        properties:
          firstName:
            type: string
          lastName:
            type: string
          username:
            type: string
      Users:
        type: array
        items:
          $ref: "#/definitions/User"
    
      Response:
        type: object
        required:
          - code
          - message
        properties:
          code:
            type: integer
            description: The success code, 0 or -1.
          message:
            type: string
            description: The description message for this success code
          errors:
            type: array
            description: A map of errors within the request. Keyed by the parameter name and the values are the error details
    
      BadRequest:
        type: object
        required:
          - validationErrors
        properties:
          validationErrors:
            type: array
            items:
              type: string
    
    responses:
      NonSuccess:
        description: Generic response for a non-success
        schema:
          $ref: "#/definitions/Response"
    
      BadRequest:
        description: Invalid request parameters
        schema:
          allOf:
            - $ref: "#/definitions/Response"
            - $ref: "#/definitions/BadRequest"