OpenApi v3: additionalProperties false with referenced schemas

Viewed 4015

There is some questions around this topic, but I dind't find the right way to do it.

What I want is to define all parameters in a single place and reuse it with no need to write it again. I already get that by using "allOf", but this limited the use of "additionalProperties".

My schema has this structure:

SchemaBase:
  type: object
  properties:
    foo:
      type: string

SchemaFull:
  allOf:
    - $ref: '#/components/schemas/SchemaBase'
    - type: object
      properties:
        bar:
          type: string

I tried using using definitions but it seems is not anymore in OpenApi version 3.

Here is a solution but its not what I'm looking for, becasue that is for properties, not the entire schema.

1 Answers

You need to use components like this:

openapi: 3.0.1
info:
  title: OAS 3
  version: 1.0.0
tags:
- name: example
paths:
  /exe:
    post:
      requestBody:
        content:
          application/json:
            schema:
              additionalProperties: false
              allOf:
                - $ref: '#/components/schemas/SchemaBase'
                - type: object
                  properties:
                    bar:
                      type: string
      responses:
        200:
          description: Foo
          content: {}
components:
  schemas:
    SchemaBase:
      type: object
      properties:
        foo:
          type: string

You may see and play with this here: https://editor.swagger.io/


JSON schema mapped:

{
  "additionalProperties": false,
  "allOf": [
    { "$ref": "#/definitions/SchemaBase" },
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string"
        }
      }
    }
  ],
  "definitions": {
    "SchemaBase": {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string"
        }
      }
    }
  }
}
Related