Skip to content

External references using anchors resulting in double references in generated typescript #1276

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
2 tasks done
pvanagtmaal opened this issue Aug 7, 2023 · 1 comment · Fixed by #1278
Closed
2 tasks done
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@pvanagtmaal
Copy link
Contributor

pvanagtmaal commented Aug 7, 2023

Description

In a slightly complex schema with multiple files and using YAML anchors to define objects, something fishy is going on with the external schemas in the generated typescript, with multiple self-references where there should be only one.

If you can point me in the right direction, I'd be more than willing to submit a PR :)

Name Version
openapi-typescript 6.4.0
Node.js 18.10.0
OS + version macOS 13

Reproduction

Add the following test files:

# anchor-with-ref-test.yaml
openapi: "3.0"
info:
  title: test
  version: "1.0"
paths:
  /:
    get:
      responses:
        200:
          description: OK
components:
  schemas:
    partialTest:
      type: object
      properties: &testProperties
        metadata:
          $ref: '#/components/schemas/metadata'
      additionalProperties: false
    test:
      type: object
      properties: *testProperties
      additionalProperties: false
    metadata:
      type: object
      additionalProperties: true
# anchor-with-ref-test-2.yaml
openapi: "3.0"
info:
  title: test
  version: "1.0"
paths:
  /:
    get:
      responses:
        200:
          description: OK
components:
  schemas:
    obj:
      $ref: './anchor-with-ref-test.yaml#/components/schemas/test'

Expected result

export interface paths {
  "/": {
    get: {
      responses: {
        /** @description OK */
        200: never;
      };
    };
  };
}
export type webhooks = Record<string, never>;
export interface components {
  schemas: {
    obj: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["test"];
  };
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: never;
}

export interface external {
  "anchor-with-ref-test.yaml": {
    paths: {
      "/": {
        get: {
          responses: {
            /** @description OK */
            200: never;
          };
        };
      };
    };
    webhooks: Record<string, never>;
    components: {
      schemas: {
        partialTest: {
          // actual: metadata?: external["anchor-with-ref-test.yaml"]["external"]["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
          metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
        };
        test: {
          // actual: metadata?: external["anchor-with-ref-test.yaml"]["external"]["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
          metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
        };
        metadata: {
          [key: string]: unknown;
        };
      };
      responses: never;
      parameters: never;
      requestBodies: never;
      headers: never;
      pathItems: never;
    };
  };
}

export type operations = Record<string, never>;

Checklist

@pvanagtmaal pvanagtmaal added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Aug 7, 2023
@pvanagtmaal
Copy link
Contributor Author

Seems like the anchor doesn't even have to be referenced, as long as you use it somewhere...
The anchor-with-ref-test.yaml would thus become

openapi: "3.0"
info:
  title: test
  version: "1.0"
paths:
  /:
    get:
      responses:
        200:
          description: OK
components:
  schemas:
    test:
      type: object
      properties: &testProperties
        metadata:
          $ref: '#/components/schemas/metadata'
      additionalProperties: false
    anchorTest:
      type: object
      additionalProperties: false
      properties: *testProperties
    metadata:
      type: object
      additionalProperties: true

and the output would still have the double reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant