Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Mutual exclusion by use of additionalProperties: false across union types #803

Open
segfault87 opened this issue Mar 4, 2024 · 1 comment
Labels
kind/bug Indicates an issue

Comments

@segfault87
Copy link

segfault87 commented Mar 4, 2024

Actual Behavior

If additionalProperties: false is declared in components and being combined into one by allOf, validation unconditionally fails.

Let's say we have following OpenAPI spec:

---
openapi: 3.0.0

info:
  title: Test API
  description: Test
  version: 0.0.1

servers:
  - url: https://www.example.com

paths:
  /test:
    post:
      summary: test
      description: test
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Derived'
        required: true
      responses:
        "200":
          description: Success

components:
  schemas:
    Base:
      required:
      - foo
      type: object
      properties:
        foo:
          type: string
          nullable: false
      additionalProperties: false
    Derived:
      type: object
      allOf:
      - $ref: '#/components/schemas/Base'
      - type: object
        required:
        - bar
        properties:
          bar:
            type: string
            nullable: false
        additionalProperties: false

This spec compiles without any problem. But when validating actual request with this spec, validator fails with following error message:

RequestValidationResult(errors=[InvalidSchemaValue(value={'bar': '2',
                                                          'foo': '1'},
                                                   type='object',
                                                   schema_errors=(<ValidationError: "Additional properties are not allowed ('bar' was unexpected)">,
                                                                  <ValidationError: "Additional properties are not allowed ('foo' was unexpected)">))],
                        body=None,
                        parameters=Parameters(query={},
                                              header={},
                                              cookie={},
                                              path={}),
                        security={})

Expected Behavior

I think openapi-core should behave one of following:

  • Validation should be successful
  • Or forbid use of additionalProperties: false in context of union types (allOf) at schema lvel.

Steps to Reproduce

Validate {"foo": "-", "bar": "-"} with spec supplied above.

OpenAPI Core Version

0.19.0

OpenAPI Core Integration

pydantic

Affected Area(s)

validation

References

No response

Anything else we need to know?

No response

Would you like to implement a fix?

Yes

@segfault87 segfault87 added the kind/bug Indicates an issue label Mar 4, 2024
@p1c2u
Copy link
Collaborator

p1c2u commented Mar 16, 2024

Hi @segfault87 thanks for the report.

Mutual exclusion is not something that should be checked by validation tool. Validation tools just make sure your requirements are met. Is up to user how he design his requirements. Tools shouldn't forbid to make mutual exclusion requirement like interest > 10 and < 1

What you need is probably unevaluatedProperties which part of OpenAPI 3.1

components:
  schemas:
    Base:
      required:
      - foo
      type: object
      properties:
        foo:
          type: string
          nullable: false
    Derived:
      type: object
      unevaluatedProperties: false
      allOf:
      - $ref: '#/components/schemas/Base'
      - type: object
        required:
        - bar
        properties:
          bar:
            type: string
            nullable: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Indicates an issue
Projects
None yet
Development

No branches or pull requests

2 participants