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

Audit request fails when using yaml anchors #100

Closed
quezak opened this issue Nov 24, 2020 · 4 comments
Closed

Audit request fails when using yaml anchors #100

quezak opened this issue Nov 24, 2020 · 4 comments

Comments

@quezak
Copy link

quezak commented Nov 24, 2020

Issue

In my components/schemas I'm sometimes using a YAML &anchor and its merge operator (<<: *anchor), notably in object property definitions, to avoid repetitions when one object type extends another. If such anchors are used, requesting the security audit fails:

1.If the anchor is used in a $referenced separate file, as pasted below, the error says: image
2. If I merge the files below instead of $referencing the other file, the error says: image

The SwaggerUI preview rendering in the extension works fine, so does using the openapi-generator on such a spec.

If I delete the anchor, and just copy the first object's properties into the second object's properties, the extension works correctly.

Minimal example

  1. test-api.yml
openapi: '3.0.3'

info:
  version: 0.0.1
  title: Test

paths:
  /foo:
    $ref: './paths-foo.yml#/paths/~1foo'

security:
  - ApiKeyAuth: []

components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Api-Key
  1. paths-foo.yml
openapi: '3.0.3'

info:
  version: 0.0.1
  title: test paths -- see main file test-api.yml

paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewTestObj'
      responses:
        '201':
          description: A created test object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestObj'

components:
  schemas:
    NewTestObj:
      type: object
      properties: &NewTestObjProperties
        name:
          type: string
        size:
          type: number
      required:
        - name
    
    TestObj:
      type: object
      properties:
        <<: *NewTestObjProperties
        id:
          type: string
          format: uuid
      required:
        - id
        - name
@quezak
Copy link
Author

quezak commented Nov 24, 2020

SwaggerUI renders correctly even when using the anchor:
image

@ak1394
Copy link
Collaborator

ak1394 commented Nov 25, 2020

Thanks for the detailed example, will take a look!

@ak1394
Copy link
Collaborator

ak1394 commented Dec 3, 2020

This is fixed in the next release (should be published in a couple of days).

I would personally advice against using YAML anchors as it adds additional level of complexity to OpenAPI.

Using http://json-schema.org/understanding-json-schema/reference/combining.html (and taking https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#schema-object into consideration) is better, as it would not hide the source of schema definitions from the tools which validate JSON schemas and are not aware of YAML anchors.

@ak1394 ak1394 closed this as completed Dec 3, 2020
@quezak
Copy link
Author

quezak commented Dec 3, 2020

@ak1394 thanks for the fix!

I would personally advice against using YAML anchors as it adds additional level of complexity to OpenAPI.

Yeah I know, so I'm using the anchors only in really obvious case where they don't impact schema readability and validation, like the one I posted.

Unfortunately if I understand correctly the JSON-schema combinators don't work in this case, because (1) they're poorly supported in generators (or not at all in my case (typescript)), (2) they work a bit different -- even the docs say that allOf can not be used to “extend” a schema to add more detail, and they have some proposals that do this for next versions.

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

2 participants