lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.54k stars 499 forks source link

Escape backtick and template variable from type name #1469

Closed MacFJA closed 10 months ago

MacFJA commented 1 year ago

All Submissions:


This PR solve the issue with Swagger that can't handle $ schema name.

My use case is the following:

type MyModel = {
  normalProp: string;
  [key: `variable${number}`]: string;
}

TSOA can't use the type MyModal which is understandable as one of the key is dynamic. My approach what to create a TSOA specific type like this:

import { MyModal as SourceMyModel } from "./types";
type MyModel = Omit<SourceMyModel, `variable${number}`> & { variable1: string, variable2: string };

With this, TSOA is able to generate the OpenApi file, but the generated schemas are:

"Pick_SourceMyModel.Exclude_keyofSourceMyModel.%60variable%24_number_%60__": {
    "properties": {
        "normalProp": {
            "type": "string"
        }
    },
    "required": [
        "normalProp"
    ],
    "type": "object",
    "description": "From T, pick a set of properties whose keys are in the union K"
},
"Omit_SourceMyModel.%60variable%24_number_%60_": {
    "$ref": "#/components/schemas/Pick_SourceMyModel.Exclude_keyofSourceMyModel.%60variable%24_number_%60__",
    "description": "Construct a type with the properties of T except for those in type K."
},
"MyModel": {
    "allOf": [
        {
            "$ref": "#/components/schemas/Omit_SourceMyModel.%60variable%24_number_%60_"
        },
        {
            "properties": {
                "variable2": {
                    "type": "string"
                },
                "variable1": {
                    "type": "string"
                }
            },
            "type": "object"
        }
    ]
}

With my PR the output will be:

"Pick_SourceMyModel.Exclude_keyofSourceMyModel.variable_number__": {
    "properties": {
        "normalProp": {
            "type": "string"
        }
    },
    "required": [
        "normalProp"
    ],
    "type": "object",
    "description": "From T, pick a set of properties whose keys are in the union K"
},
"Omit_SourceMyModel.variable_number_": {
    "$ref": "#/components/schemas/Pick_SourceMyModel.Exclude_keyofSourceMyModel.variable_number__",
    "description": "Construct a type with the properties of T except for those in type K."
},
"MyModel": {
    "allOf": [
        {
            "$ref": "#/components/schemas/Omit_SourceMyModel.variable_number_"
        },
        {
            "properties": {
                "variable2": {
                    "type": "string"
                },
                "variable1": {
                    "type": "string"
                }
            },
            "type": "object"
        }
    ]
}
WoH commented 1 year ago

Looks reasonable, can you please add the example you provided as a test?

MacFJA commented 1 year ago

@WoH Sorry for the delay. I added the test, I hope I put it in the right place

amrbahaa360 commented 1 year ago

Is the PR merged to version 5.1.1? The issue still exists and has not been solved.

WoH commented 1 year ago

Is the PR merged to version 5.1.1? The issue still exists and has not been solved.

Would you like to improve on this PR and address the points I raised during review? Remember to add tests.

github-actions[bot] commented 11 months ago

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 10 months ago

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days