Skip to content

Commit

Permalink
fix: replace # characters in operation IDs with a slash (#1545)
Browse files Browse the repository at this point in the history
* fix: replace # characters in operation IDs with a slash

Fixes #1542

* add hash regex constant
  • Loading branch information
jaredLunde committed Feb 19, 2024
1 parent 8aab256 commit 9158b81
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-dots-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": minor
---

Works around an issue where operation IDs containing the "#" character failed to generate teh correct type mappings
10 changes: 6 additions & 4 deletions packages/openapi-typescript/src/transform/path-item-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ export default function transformPathItemObject(
}
// if operationId exists, move into an `operations` export and pass the reference in here
else if (operationObject.operationId) {
operationType = oapiRef(
createRef(["operations", operationObject.operationId]),
);
// workaround for issue caused by redocly ref parsing: https://github.com/drwpow/openapi-typescript/issues/1542
const operationId = operationObject.operationId.replace(HASH_RE, "/");
operationType = oapiRef(createRef(["operations", operationId]));
injectOperationObject(
operationObject.operationId,
operationId,
{ ...operationObject, parameters: Object.values(keyedParameters) },
{ ...options, path: createRef([options.path!, method]) },
);
Expand All @@ -132,3 +132,5 @@ export default function transformPathItemObject(

return ts.factory.createTypeLiteralNode(type);
}

const HASH_RE = /#/g;
78 changes: 78 additions & 0 deletions packages/openapi-typescript/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,84 @@ export type operations = Record<string, never>;`,
// options: DEFAULT_OPTIONS
},
],

[
"operations > # character is parsed correctly",
{
given: {
openapi: "3.1",
info: { title: "Test", version: "1.0" },
paths: {
"/accounts": {
get: {
operationId: "Accounts#List",
responses: {
200: {
description: "OK",
content: {
"application/json": {
schema: { type: "string" },
},
},
},
},
},
},
},
},
want: `export interface paths {
"/accounts": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: operations["Accounts/List"];
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
}
export type webhooks = Record<string, never>;
export interface components {
schemas: never;
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}
export type $defs = Record<string, never>;
export interface operations {
"Accounts/List": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description OK */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": string;
};
};
};
};
}`,
// options: DEFAULT_OPTIONS,
},
],
[
"JSONSchema > $defs are respected",
{
Expand Down

0 comments on commit 9158b81

Please sign in to comment.