Skip to content

OpenAPI 3.0 spec not parsing required properly #27

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

Closed
viralanomaly opened this issue Jun 19, 2018 · 9 comments
Closed

OpenAPI 3.0 spec not parsing required properly #27

viralanomaly opened this issue Jun 19, 2018 · 9 comments

Comments

@viralanomaly
Copy link

I have a spec with components that look like this:


components:
  schemas:
    Credit:
      type: object
      properties:
        clientId:
          type: string
    CreditCreate:
      allOf:
        - $ref: '#/components/schemas/Credit'
        - required:
          - clientId

I'm doing it like this because there are about 30 fields in credit, and 10 in required. All the fields in required are present in the linked object (or objects if I'm linking more than one). This is rejected by the code in validators.py inside SchemaValidator. iter_errors calls itself recursively on each piece of the allOf, but this doesn't let me reuse another object and simply add some fields as required. I worked around it for now by telling iter_errors to check that the properties field contains something before erroring, but I'm not sure that it is the correct fix.

This behavior is supported in the swagger editor and in redoc, so I'm working off the assumption that it should be valid 3.0 syntax.

@dtkav
Copy link
Contributor

dtkav commented Jun 20, 2018

@viralanomaly after looking through the OpenAPI3.0.1 spec section on schemaObject and the json schema validation rfc, I believe that each allOf schema must be able valid independently from each other.

see the excerpts below (emphasis mine)

from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#schemaObject

allOf - Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.

and

The following properties are taken directly from the JSON Schema definition and follow the same specifications:
...
required
...

from http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.3

6.5.3. required

The value of this keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

I think you need to supply the properties twice in your spec (example below).

openapi: 3.0.0
info:
  title: Test Api
  version: 0.0.1
paths: {}
components:
  schemas:
    Credit:
      type: object
      properties:
        clientId:
          type: string
    CreditCreate:
      allOf:
      - "$ref": "#/components/schemas/Credit"
      - required:
        - clientId
        properties:
          clientId:
            type: string

@dtkav
Copy link
Contributor

dtkav commented Jun 20, 2018

hmm, some comments by the OpenAPI folks that contrast with my conclusion above:
OAI/OpenAPI-Specification#1562
OAI/OpenAPI-Specification#1590

@dtkav
Copy link
Contributor

dtkav commented Jun 20, 2018

I heard back from one of the core jsonschema developers, and your request is the intended behavior. I put up #28 as a fix. Let me know what you think!

@p1c2u
Copy link
Collaborator

p1c2u commented Jun 21, 2018

Should be resolved with fix #28 . Feel free to reopen it otherwise.

@p1c2u p1c2u closed this as completed Jun 21, 2018
@cziebuhr
Copy link

cziebuhr commented Nov 7, 2018

I had a similar issue with following spec:

definitions:
  User:
    type: object
    properties:
      id:
        type: integer
       username:
         type: string
       password:
         type: string
  GetUser:
    required:
    - id
    - username
    allOf:
    - $ref: '#/definitions/User'
  PostUser:
    required:
    - username
    - password
    allOf:
    - $ref: '#/definitions/User'

The difference is, that I had required on top-level and not as child of allOf.
I see no point in swagger spec / json schema that this is invalid. Each validation keyword gets validated on its own. It's even valid to have a required keyword without having type: object or the instance being an object. In the latter case it just gets ignored, which means it succeeds.
I therefore vote for totally removing ExtraParametersError.

@dtkav
Copy link
Contributor

dtkav commented Nov 7, 2018 via email

@cziebuhr
Copy link

cziebuhr commented Nov 8, 2018

This is just a simplified example, I already use readOnly/writeOnly, but also have a PatchUser where nothing is required.

@codyaray
Copy link

codyaray commented Jun 9, 2020

I also ran into this when trying to re-use an explicit *Required schema instead of inlining the same group of fields everywhere this is needed (like @cziebuhr , that's everywhere except the PATCH request)

anyOf:
- $ref: #/components/schemas/MyObject
- $ref: #/components/schemas/MyObjectRequired

components:
  schemas:
    MyObjectRequired:
      type: object
      required:
      - field1
      - field2

@flixx
Copy link

flixx commented Jun 3, 2021

Hello.
For me it looks like the issue is not really resolved. It's somewhat ignoreing the required property now without giving the expected result. As far as I understand it's also related and caused by how openAPI is specified (See OAI/OpenAPI-Specification#1608 )

I think It would be cool if this issue here could be reopened... It would be less confusing for people like me who find this thread on google and figure out if it's working or not.

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

No branches or pull requests

6 participants