Skip to content

use the same openapi, json file generate ok, but yaml file generate error #1093

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
1 task done
liangskyli opened this issue May 8, 2023 · 12 comments · Fixed by #1146
Closed
1 task done

use the same openapi, json file generate ok, but yaml file generate error #1093

liangskyli opened this issue May 8, 2023 · 12 comments · Fixed by #1146
Labels
openapi-ts Relevant to the openapi-typescript library question Further information is requested

Comments

@liangskyli
Copy link
Contributor

liangskyli commented May 8, 2023

Description

the same openapi:
when use openapi.yaml have a generate bug.
when use openapi.json no generate bug.

use openapi.yaml generate error data:

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


export interface paths {
  "/root/v4/getQueryParams1-v4": {
    get: {
      parameters: {
        query: {
          /** @description activityBases */
          activityBases: (external["components[%22schemas%22][%22ActivityBase%22]"])[];
        };
      };
      responses: {
        /** @description Success */
        200: {
          content: {
            "text/plain": string;
          };
        };
      };
    };
  };
}

export type webhooks = Record<string, never>;

export interface components {
  schemas: {
    getQueryParams1Request: {
      /** @description activityBases */
      activityBases: (external["components[%22schemas%22][%22ActivityBase%22]"])[];
    };
    ActivityBase: {
      name: string;
    } & {
      id: number;
    };
  };
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: never;
}

export type external = Record<string, never>;

export type operations = Record<string, never>;
Name Version
openapi-typescript 6.2.4
Node.js 16.14.0
OS + version macOS 13, Windows 10, etc.

Reproduction

How can this be reproduced / when did the error occur?

openapi.json

{
  "openapi": "3.1.0",
  "info": {
    "title": "custom title",
    "license": {
      "name": "This file is auto generated, do not edit!"
    },
    "version": "0.4.1"
  },
  "paths": {
    "/root/v4/getQueryParams1-v4": {
      "get": {
        "tags": ["Test4"],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "name": "activityBases",
            "in": "query",
            "required": true,
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/ActivityBase"
              }
            },
            "description": "activityBases"
          }
        ]
      }
    }
  },
  "components": {
    "schemas": {
      "getQueryParams1Request": {
        "type": "object",
        "properties": {
          "activityBases": {
            "description": "activityBases",
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ActivityBase"
            }
          }
        },
        "required": ["activityBases"]
      },
      "ActivityBase": {
        "allOf": [
          {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              }
            },
            "required": ["name"]
          },
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "number"
              }
            },
            "required": ["id"]
          }
        ]
      }
    }
  },
  "tags": [
    {
      "name": "Test4",
      "description": "Test3Controller 注释\n注释3"
    }
  ]
}

openapi.yaml

openapi: 3.1.0
info:
  title: custom title
  license:
    name: This file is auto generated, do not edit!
  version: 0.4.1
paths:
  /root/v4/getQueryParams1-v4:
    get:
      tags:
        - Test4
      responses:
        '200':
          description: Success
          content:
            text/plain:
              schema:
                type: string
      parameters:
        - name: activityBases
          in: query
          required: true
          schema:
            type: array
            items: &ref_0
              $ref: '#/components/schemas/ActivityBase'
          description: activityBases
components:
  schemas:
    getQueryParams1Request:
      type: object
      properties:
        activityBases:
          description: activityBases
          type: array
          items: *ref_0
      required:
        - activityBases
    ActivityBase:
      allOf:
        - type: object
          properties:
            name:
              type: string
          required:
            - name
        - type: object
          properties:
            id:
              type: number
          required:
            - id
tags:
  - name: Test4
    description: |-
      Test3Controller 注释
      注释3

Expected result

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


export interface paths {
  "/root/v4/getQueryParams1-v4": {
    get: {
      parameters: {
        query: {
          /** @description activityBases */
          activityBases: (components["schemas"]["ActivityBase"])[];
        };
      };
      responses: {
        /** @description Success */
        200: {
          content: {
            "text/plain": string;
          };
        };
      };
    };
  };
}

export type webhooks = Record<string, never>;

export interface components {
  schemas: {
    getQueryParams1Request: {
      /** @description activityBases */
      activityBases: (components["schemas"]["ActivityBase"])[];
    };
    ActivityBase: {
      name: string;
    } & {
      id: number;
    };
  };
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: never;
}

export type external = Record<string, never>;

export type operations = Record<string, never>;

Checklist

@drwpow
Copy link
Contributor

drwpow commented May 9, 2023

I’ll take a look at this. Thank you for submitting a PR! But I fear that may not fix the underlying issue here, and I’d like to take a different approach.

@liangskyli
Copy link
Contributor Author

The reason for this bug is:
use items: &ref_0 and items: *ref_0, Causes the ref reference in the currentSchema to point to the same address, and the currentSchema data has changed, resulting in ref transform many times!
I use JSON.parse and JSON.stringify to handle schema data before walk currentSchema to refactor code.
@drwpow : I have changed the implementation. Do you have time to take a look at this plan?

@liangskyli
Copy link
Contributor Author

The core reason is due to changes in the value of currentSchema,
I haven't had time to deal with this in detail lately, I'm quite busy. If you have time, you can check if there's a better solution. Thank you!

@drwpow
Copy link
Contributor

drwpow commented May 13, 2023

Thanks for the update. I plan on digging into this more this weekend myself!

@drwpow
Copy link
Contributor

drwpow commented May 14, 2023

OK I’m digging into this for the first time, and I think I’m missing something:

          schema:
            type: array
            items: &ref_0
              $ref: '#/components/schemas/ActivityBase'

How is this valid YAML?

      properties:
        activityBases:
          description: activityBases
          type: array
          items: *ref_0

How is this a valid OpenAPI schema?

When I try and validate this schema, I get:

Error parsing /Users/drew/Sites/drwpow/openapi-typescript/packages/openapi-typescript/test.yaml: unidentified alias "ref_0" at line 36, column 24:
              items: *ref_0

Could you try updating your example with a YAML schema that:

  • is valid YAML, and
  • passes OpenAPI 3.1 validation? (e.g. Swagger CLI)?

@drwpow drwpow added the question Further information is requested label May 14, 2023
@liangskyli
Copy link
Contributor Author

liangskyli commented May 14, 2023

this is valid YAML! openapi.yaml generate by js-yaml

 import { dump } from 'js-yaml'; 
// example data
dump({
"openapi": "3.1.0",
......
// other data
});

then generate yaml file!

image

see: https://yaml.org/spec/1.2.2/

@liangskyli
Copy link
Contributor Author

YAML: & Node Anchors
image

@liangskyli
Copy link
Contributor Author

@liangskyli
Copy link
Contributor Author

when i use, noRefs true, it will not use & Node Anchors generate yaml file

import { dump } from 'js-yaml';

dump({obj data}),{noRefs: true});

Now I have temporarily added this configuration to the generated yaml file.

@drwpow drwpow added the openapi-ts Relevant to the openapi-typescript library label May 22, 2023
@liangskyli
Copy link
Contributor Author

Excuse me, how is the progress in handling this issue? Do you have a better plan?

@drwpow
Copy link
Contributor

drwpow commented May 25, 2023

Hello! Yes I’ve pinpointed the issue. And I can fix it easily. Will comment thoughts on the PR.

@drwpow drwpow mentioned this issue May 25, 2023
3 tasks
@drwpow
Copy link
Contributor

drwpow commented May 25, 2023

This has been fixed in v6.2.6!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-ts Relevant to the openapi-typescript library question Further information is requested
Projects
None yet
2 participants