Skip to content

Commit

Permalink
Merge pull request #1468 from LucianBuzzo/lucianbuzzo/1461-urlencode-…
Browse files Browse the repository at this point in the history
…in-type-names

fix: Don't URI encode schema keys when using $ref
  • Loading branch information
WoH authored Sep 4, 2023
2 parents 4f452f9 + 4305e20 commit d7cf119
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/metadataGeneration/metadataGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class MetadataGenerator {
}

private setProgramToDynamicControllersFiles(controllers: string[], esm: boolean) {
const allGlobFiles = importClassesFromDirectories(controllers, esm ? ['.mts', '.ts', '.cts']: ['.ts']);
const allGlobFiles = importClassesFromDirectories(controllers, esm ? ['.mts', '.ts', '.cts'] : ['.ts']);
if (allGlobFiles.length === 0) {
throw new GenerateMetadataError(`[${controllers.join(', ')}] globs found 0 controllers.`);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ export class MetadataGenerator {
if (!referenceType.refName) {
return;
}
this.referenceTypeMap[referenceType.refName] = referenceType;
this.referenceTypeMap[decodeURIComponent(referenceType.refName)] = referenceType;
}

public GetReferenceType(refName: string) {
Expand Down
28 changes: 13 additions & 15 deletions packages/cli/src/metadataGeneration/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,21 +763,19 @@ export class TypeResolver {
}

private getRefTypeName(name: string): string {
return encodeURIComponent(
name
.replace(/<|>/g, '_')
.replace(/\s+/g, '')
.replace(/,/g, '.')
.replace(/'([^']*)'/g, '$1')
.replace(/"([^"]*)"/g, '$1')
.replace(/&/g, '-and-')
.replace(/\|/g, '-or-')
.replace(/\[\]/g, '-Array')
.replace(/{|}/g, '_') // SuccessResponse_{indexesCreated-number}_ -> SuccessResponse__indexesCreated-number__
.replace(/([a-z]+):([a-z]+)/gi, '$1-$2') // SuccessResponse_indexesCreated:number_ -> SuccessResponse_indexesCreated-number_
.replace(/;/g, '--')
.replace(/([a-z]+)\[([a-z]+)\]/gi, '$1-at-$2'), // Partial_SerializedDatasourceWithVersion[format]_ -> Partial_SerializedDatasourceWithVersion~format~_,
);
return name
.replace(/<|>/g, '_')
.replace(/\s+/g, '')
.replace(/,/g, '.')
.replace(/'([^']*)'/g, '$1')
.replace(/"([^"]*)"/g, '$1')
.replace(/&/g, '-and-')
.replace(/\|/g, '-or-')
.replace(/\[\]/g, '-Array')
.replace(/{|}/g, '_') // SuccessResponse_{indexesCreated-number}_ -> SuccessResponse__indexesCreated-number__
.replace(/([a-z]+):([a-z]+)/gi, '$1-$2') // SuccessResponse_indexesCreated:number_ -> SuccessResponse_indexesCreated-number_
.replace(/;/g, '--')
.replace(/([a-z]+)\[([a-z]+)\]/gi, '$1-at-$2'); // Partial_SerializedDatasourceWithVersion[format]_ -> Partial_SerializedDatasourceWithVersion~format~_,
}

private attemptToResolveKindToPrimitive = (syntaxKind: ts.SyntaxKind): ResolvesToPrimitive | DoesNotResolveToPrimitive => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/swagger/specGenerator2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export class SpecGenerator2 extends SpecGenerator {
}

protected getSwaggerTypeForReferenceType(referenceType: Tsoa.ReferenceType): Swagger.BaseSchema {
return { $ref: `#/definitions/${referenceType.refName}` };
return { $ref: `#/definitions/${encodeURIComponent(referenceType.refName)}` };
}

private decideEnumType(anEnum: Array<string | number>, nameOfEnum: string): 'string' | 'number' {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export class SpecGenerator3 extends SpecGenerator {
}

protected getSwaggerTypeForReferenceType(referenceType: Tsoa.ReferenceType): Swagger.BaseSchema {
return { $ref: `#/components/schemas/${referenceType.refName}` };
return { $ref: `#/components/schemas/${encodeURIComponent(referenceType.refName)}` };
}

protected getSwaggerTypeForPrimitiveType(dataType: Tsoa.PrimitiveTypeLiteral): Swagger.Schema {
Expand Down

0 comments on commit d7cf119

Please sign in to comment.