OpenApi v3: additionalProperties false with referenced schemas
Asked Answered
M

2

9

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.

Myrlemyrlene answered 5/6, 2020 at 9:56 Comment(0)
K
1

With OpenAPI specification version 3.1.0 you can use unevaluatedProperties in combination with allOf as follows:

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

This should produce wanted behavior and validate additional properties against both schemas.

Kalgan answered 2/9, 2024 at 18:48 Comment(0)
L
-1

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"
        }
      }
    }
  }
}
Laurelaureano answered 9/6, 2020 at 9:16 Comment(5)
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.Hollander
What validator are you using? - I have added the additionalProperties: false, and that is a valid OAS schemaLaurelaureano
I'm using express-openapi, and additionalProperties: false works on it, but not along with allOf, because only validate one schema or another.Hollander
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.Hollander
Understood, I have added the corresponding schema and recreated the problem using jsonschemavalidator.net. I will give it a tryLaurelaureano

© 2022 - 2025 — McMap. All rights reserved.