7

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 Answers1

0

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"
        }
      }
    }
  }
}
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • 2
    Sorry, may be I didn't explain myself. What I want is to use "additionalProperties: false" to validate a union of schemas, but it seems it isn't possible. I already tried with sevaral different combination, but I didn't make it works. – Gabriel Rodríguez Flores Jun 09 '20 at 10:19
  • What validator are you using? - I have added the `additionalProperties: false`, and that is a valid OAS schema – Manuel Spigolon Jun 09 '20 at 11:05
  • 2
    I'm using express-openapi, and `additionalProperties: false` works on it, but not along with `allOf`, because only validate one schema or another. – Gabriel Rodríguez Flores Jun 10 '20 at 06:52
  • 2
    What I say is that schema won't work, it's syntactically correct, but it will check that none parameters are valid, just because `additionalProperties` work at siblings level, and no enter inside of `allOf`. – Gabriel Rodríguez Flores Jun 10 '20 at 07:01
  • Understood, I have added the corresponding schema and recreated the problem using https://www.jsonschemavalidator.net/. I will give it a try – Manuel Spigolon Jun 10 '20 at 07:43