hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.36k stars 105 forks source link

Automatic addition of the suffix `Schema` to the object names in `schemas.gen.ts` causes conflicts with `types.gen.ts` #1059

Open seperman opened 2 months ago

seperman commented 2 months ago

Description

Automatic addition of the suffix Schema to the object names in schemas.gen.ts causes conflicts with types.gen.ts

In the openapi specs we have:

{
    "WarningAction":
    {
        "type": "string",
        "enum":
        [
            "set_value_to_null_once",
            "set_value_to_null",
            "set_value_to_null_for_field",
            "hide"
        ],
        "title": "WarningAction",
        "description": "Warning Actions are a subset of Alert Actions"
    },
    "WarningActionSchema":
    {
        "properties":
        {
            "text":
            {
                "type": "string",
                "title": "Text"
            },
            "info":
            {
                "type": "string",
                "title": "Info"
            },
            "action":
            {
                "$ref": "#/components/schemas/WarningAction"
            },
            "url":
            {
                "anyOf":
                [
                    {
                        "type": "string"
                    },
                    {
                        "type": "null"
                    }
                ],
                "title": "Url"
            }
        },
        "type": "object",
        "required":
        [
            "text",
            "info",
            "action"
        ],
        "title": "WarningActionSchema"
    },
}

But heyapi creates:

schemas.gen.ts:

export const WarningActionSchema = {
    type: 'string',
    enum: ['set_value_to_null_once', 'set_value_to_null', 'set_value_to_null_for_field', 'hide'],
    title: 'WarningAction',
    description: `Warning Actions are a subset of Alert Actions`
} as const;

export const WarningActionSchemaSchema = {
    properties: {
        text: {
            type: 'string',
            title: 'Text'
        },
        info: {
            type: 'string',
            title: 'Info'
        },
        action: {
            '$ref': '#/components/schemas/WarningAction'
        },
        url: {
            anyOf: [
                {
                    type: 'string'
                },
                {
                    type: 'null'
                }
            ],
            title: 'Url'
        }
    },
    type: 'object',
    required: ['text', 'info', 'action'],
    title: 'WarningActionSchema'
} as const;

And in types.gen.ts:

export type WarningAction = 'set_value_to_null_once' | 'set_value_to_null' | 'set_value_to_null_for_field' | 'hide';

export type WarningActionSchema = {
    text: string;
    info: string;
    action: WarningAction;
    url?: (string | null);
};

Which causes a conflict because WarningActionSchema has a conflict between the 2 files.

OpenAPI specification (optional)

openapi: 3.0.0 info: title: Warning Actions API version: 1.0.0 description: API to handle warning actions servers:

System information (optional)

No response

stackblitz[bot] commented 2 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

seperman commented 2 months ago

Also manually setting the schema name pattern can fix the issue, but I think openapi-ts should detect these conflicts and use a proper suffix or prefix automatically.

export default defineConfig({
  client: '@hey-api/client-axios',
  input: 'openapi.yaml',
  output: {
    format: 'prettier',
    path: './src/client',
  },
  types: {
    dates: 'types+transform',
    enums: 'javascript',
  },
  schemas: {
    name: (name) => `_${name}`, // [!code ++]
  },
});
mrlubos commented 2 months ago

@seperman How would you choose to resolve this conflict if you were asked to resolve it manually?

mrlubos commented 4 weeks ago

This will be fixed by removing the index.ts barrel file, the complexity of maintaining a conflict-free index file is not worth it.