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

Issues with references ($ref) resolution #1658

Closed
2 tasks done
Ettapp opened this issue May 10, 2024 · 2 comments
Closed
2 tasks done

Issues with references ($ref) resolution #1658

Ettapp opened this issue May 10, 2024 · 2 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@Ettapp
Copy link

Ettapp commented May 10, 2024

Description

Sometimes referenced items (that are correctly resolved by Swagger editor) are output as unknown (see example A) and sometimes they are (see example B)

Name Version
openapi-typescript 6.7.5
Node.js 20.12.2
OS + version Ubuntu 23.10

Reproduction

Example A

main.yaml

openapi: 3.0.0
info:
  title: Title
  version: 1.0.0

paths:
  /secondary:
    $ref: 'secondary.yaml#/secondaryPath'

secondary.yaml

components:
  schemas:
    # Issue with the resolution of this schema
    secondaryNestedSchema:
      type: number

# This one works fine
secondaryRootSchema:
  type: string

secondaryPath:
  get:
    tags:
      - Playlist
    responses:
      '200':
        content:
          application/json:
            schema:
              type: object
              properties:
                rootSchema:
                  $ref: '#/secondaryRootSchema'
                nestedSchema:
                  $ref: '#/components/schemas/secondaryNestedSchema'

npx openapi-typescript main.yaml will output:

/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */


export interface paths {
  "/secondary": external["secondary.yaml"]["secondaryPath"];
}

export type webhooks = Record<string, never>;

export type components = Record<string, never>;

export type $defs = Record<string, never>;

export interface external {
  "secondary.yaml": {
    components: unknown;
    secondaryRootSchema: string;
    secondaryPath: {
      get: {
        responses: {
          200: {
            content: {
              "application/json": {
                rootSchema?: external["secondary.yaml"]["secondaryRootSchema"];
                // Error next line: "Property 'schemas' does not exist on type 'unknown'."
                nestedSchema?: external["secondary.yaml"]["components"]["schemas"]["secondaryNestedSchema"];
              };
            };
          };
        };
      };
    };
  };
}

export type operations = Record<string, never>;

Example B

main.yaml

openapi: 3.0.0
info:
  title: Title
  version: 1.0.0

paths:
  /secondary:
    $ref: 'secondary.yaml#/paths/secondaryPath'

secondary.yaml

components:
  schemas:
    secondaryNestedSchema:
      type: number

secondaryRootSchema:
  type: string

paths:
  secondaryPath:
    get:
      tags:
        - Playlist
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  rootSchema:
                    $ref: '#/secondaryRootSchema'
                  nestedSchema:
                    $ref: '#/components/schemas/secondaryNestedSchema'

npx openapi-typescript main.yaml will output:

/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */


export interface paths {
  "/secondary": external["secondary.yaml"]["paths"]["secondaryPath"];
}

export type webhooks = Record<string, never>;

export type components = Record<string, never>;

export type $defs = Record<string, never>;

export interface external {
  "secondary.yaml": {
    paths: {
      "secondaryPath": {
        get: {
          responses: {
            200: {
              content: {
                "application/json": {
                  // two errors here:
                  // "'rootSchema' is referenced directly or indirectly in its own type annotation."
                  // "'Property 'secondaryRootSchema' does not exist on type..."
                  rootSchema?: external["secondary.yaml"]["secondaryRootSchema"];
                  nestedSchema?: external["secondary.yaml"]["components"]["schemas"]["secondaryNestedSchema"];
                };
              };
            };
          };
        };
      };
    };
    webhooks: Record<string, never>;
    components: {
      schemas: {
        secondaryNestedSchema: number;
      };
      responses: never;
      parameters: never;
      requestBodies: never;
      headers: never;
      pathItems: never;
    };
    $defs: Record<string, never>;
  };
}

export type operations = Record<string, never>;

Checklist

Footnotes

  1. For the sake of simplicity, I've excluded fields like servers, summary, description, security from the examples, so Redocly warns about it, but I've tested with all the required / recommended fields and the issues still arises.

  2. I'll try to find the source of the issue and to submit a PR but if anyone that has better knowledge of the codebase knows how to fix this, do not wait on me ^^

@Ettapp Ettapp added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels May 10, 2024
@fitztrev
Copy link

I was getting this too but on v7 it seems to be working

@Ettapp
Copy link
Author

Ettapp commented May 14, 2024

It works on v7 indeed, I've somehow missed the existence of the @next version, my bad.

@Ettapp Ettapp closed this as completed May 14, 2024
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

No branches or pull requests

2 participants