Skip to content

Commit 9158b81

Browse files
authored
fix: replace # characters in operation IDs with a slash (#1545)
* fix: replace # characters in operation IDs with a slash Fixes #1542 * add hash regex constant
1 parent 8aab256 commit 9158b81

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

.changeset/olive-dots-sing.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": minor
3+
---
4+
5+
Works around an issue where operation IDs containing the "#" character failed to generate teh correct type mappings

packages/openapi-typescript/src/transform/path-item-object.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ export default function transformPathItemObject(
104104
}
105105
// if operationId exists, move into an `operations` export and pass the reference in here
106106
else if (operationObject.operationId) {
107-
operationType = oapiRef(
108-
createRef(["operations", operationObject.operationId]),
109-
);
107+
// workaround for issue caused by redocly ref parsing: https://github.com/drwpow/openapi-typescript/issues/1542
108+
const operationId = operationObject.operationId.replace(HASH_RE, "/");
109+
operationType = oapiRef(createRef(["operations", operationId]));
110110
injectOperationObject(
111-
operationObject.operationId,
111+
operationId,
112112
{ ...operationObject, parameters: Object.values(keyedParameters) },
113113
{ ...options, path: createRef([options.path!, method]) },
114114
);
@@ -132,3 +132,5 @@ export default function transformPathItemObject(
132132

133133
return ts.factory.createTypeLiteralNode(type);
134134
}
135+
136+
const HASH_RE = /#/g;

packages/openapi-typescript/test/index.test.ts

+78
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,84 @@ export type operations = Record<string, never>;`,
488488
// options: DEFAULT_OPTIONS
489489
},
490490
],
491+
492+
[
493+
"operations > # character is parsed correctly",
494+
{
495+
given: {
496+
openapi: "3.1",
497+
info: { title: "Test", version: "1.0" },
498+
paths: {
499+
"/accounts": {
500+
get: {
501+
operationId: "Accounts#List",
502+
responses: {
503+
200: {
504+
description: "OK",
505+
content: {
506+
"application/json": {
507+
schema: { type: "string" },
508+
},
509+
},
510+
},
511+
},
512+
},
513+
},
514+
},
515+
},
516+
want: `export interface paths {
517+
"/accounts": {
518+
parameters: {
519+
query?: never;
520+
header?: never;
521+
path?: never;
522+
cookie?: never;
523+
};
524+
get: operations["Accounts/List"];
525+
put?: never;
526+
post?: never;
527+
delete?: never;
528+
options?: never;
529+
head?: never;
530+
patch?: never;
531+
trace?: never;
532+
};
533+
}
534+
export type webhooks = Record<string, never>;
535+
export interface components {
536+
schemas: never;
537+
responses: never;
538+
parameters: never;
539+
requestBodies: never;
540+
headers: never;
541+
pathItems: never;
542+
}
543+
export type $defs = Record<string, never>;
544+
export interface operations {
545+
"Accounts/List": {
546+
parameters: {
547+
query?: never;
548+
header?: never;
549+
path?: never;
550+
cookie?: never;
551+
};
552+
requestBody?: never;
553+
responses: {
554+
/** @description OK */
555+
200: {
556+
headers: {
557+
[name: string]: unknown;
558+
};
559+
content: {
560+
"application/json": string;
561+
};
562+
};
563+
};
564+
};
565+
}`,
566+
// options: DEFAULT_OPTIONS,
567+
},
568+
],
491569
[
492570
"JSONSchema > $defs are respected",
493571
{

0 commit comments

Comments
 (0)