swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
781 stars 527 forks source link

Correctly resolve externalRefs that point to the root path #1963

Open AmateurECE opened 1 year ago

AmateurECE commented 1 year ago

Previously, if OpenAPI v3 document A loads document B, and B refers to schemas in document A, swagger-parser would not resolve references to schemas in A as internal refs, even though they refer to the root OpenAPI v3 document (document A). This would result in the creation of duplicate schemas for those schemas in document A. Now, we resolve these "external" refs as internal refs, preventing the duplication of these schemas by the ExternalRefProcessor.

This fixes issue #1961.

AmateurECE commented 1 year ago

Upon further inspection, this only fixes a portion of the failing cases. I've observed two so far--what I'll call "A-B-A," where schemas in B reference schemas in A, and A is the top-level document, but I've discovered a new one--what I'll call "A-B-C-B," where schemas in A reference schemas in B, and schemas in C reference schemas in B. An example schema is given below (not tested yet):

foo.yaml

components:
  schemas:
    Group:
      anyOf:
      - $ref: ./bar.yaml#idRef
      - $ref: ./baz.yaml#Group_v1
<...>

bar.yaml:

components:
  schemas:
    idRef:
      format: uri-reference
<...>

baz.yaml:

components:
  schemas:
    Group_v1:
      properties:
        id:
          $ref: "./bar.yaml#idRef"
<...>
AmateurECE commented 11 months ago

I believe this fixes all the issues that I have observed. I have not been able to reproduce the "A-B-C-B" case since I originally saw it, so it's entirely possible that was a fluke. I believe this PR is ready for review/merge.