Skip to content

Commit

Permalink
šŸ“ Add docs: Node.js script alternative to update OpenAPI for generateā€¦
Browse files Browse the repository at this point in the history
ā€¦d clients (#10845)
  • Loading branch information
alejsdev committed Dec 26, 2023
1 parent 4de60e1 commit 505ae06
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
14 changes: 11 additions & 3 deletions docs/en/docs/advanced/generate-clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,17 @@ But for the generated client we could **modify** the OpenAPI operation IDs right

We could download the OpenAPI JSON to a file `openapi.json` and then we could **remove that prefixed tag** with a script like this:

```Python
{!../../../docs_src/generate_clients/tutorial004.py!}
```
=== "Python"

```Python
{!> ../../../docs_src/generate_clients/tutorial004.py!}
```

=== "Node.js"

```Python
{!> ../../../docs_src/generate_clients/tutorial004.js!}
```

With that, the operation IDs would be renamed from things like `items-get_items` to just `get_items`, that way the client generator can generate simpler method names.

Expand Down
29 changes: 29 additions & 0 deletions docs_src/generate_clients/tutorial004.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as fs from "fs";

const filePath = "./openapi.json";

fs.readFile(filePath, (err, data) => {
const openapiContent = JSON.parse(data);
if (err) throw err;

const paths = openapiContent.paths;

Object.keys(paths).forEach((pathKey) => {
const pathData = paths[pathKey];
Object.keys(pathData).forEach((method) => {
const operation = pathData[method];
if (operation.tags && operation.tags.length > 0) {
const tag = operation.tags[0];
const operationId = operation.operationId;
const toRemove = `${tag}-`;
if (operationId.startsWith(toRemove)) {
const newOperationId = operationId.substring(toRemove.length);
operation.operationId = newOperationId;
}
}
});
});
fs.writeFile(filePath, JSON.stringify(openapiContent, null, 2), (err) => {
if (err) throw err;
});
});

0 comments on commit 505ae06

Please sign in to comment.