swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Correctly resolve externalRefs that point to the root path

Open AmateurECE opened this issue 2 years ago • 2 comments

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 avatar Aug 17 '23 01:08 AmateurECE

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 avatar Aug 25 '23 11:08 AmateurECE

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.

AmateurECE avatar Nov 09 '23 12:11 AmateurECE