influxdata / influxdata/oats

Oats should not use the operation (method) and path as function names

Open
#17 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
14
Forks
5
Avg merge
3d 8h
Merged PRs (30d)
1

Description

An option is to use the `operationId` parameter as the function name to allow for a plain `/` path.

As you can see in the example swagger file, `/` is used as the path, and a valid `operationId` is given. The problem comes when generating code from this as the function name for `createStuff` is `post` and for `deleteStuff`, `delete` resulting in the following error.

```sh
# yarn run oats oats/operationID.yaml > operationID.ts
(node:47346) UnhandledPromiseRejectionWarning: SyntaxError: Variable declaration expected. (164:14)
162 | }
163 |
> 164 | export const delete = (
| ^
165 | params: DeleteParams,
166 | options: RequestOptions = {}
167 | ): Promise =>
at t (/go/src/github.com/influxdata/ui/node_modules/prettier/parser-typescript.js:1:285)
at Object.parse (/go/src/github.com/influxdata/ui/node_modules/prettier/parser-typescript.js:14:180461)
at Object.parse (/go/src/github.com/influxdata/ui/node_modules/prettier/index.js:9739:19)
at coreFormat (/go/src/github.com/influxdata/ui/node_modules/prettier/index.js:13252:23)
at format (/go/src/github.com/influxdata/ui/node_modules/prettier/index.js:13510:73)
at formatWithCursor (/go/src/github.com/influxdata/ui/node_modules/prettier/index.js:13526:12)
at /go/src/github.com/influxdata/ui/node_modules/prettier/index.js:44207:15
at Object.format (/go/src/github.com/influxdata/ui/node_modules/prettier/index.js:44226:12)
at generate (/go/src/github.com/influxdata/ui/node_modules/@influxdata/oats/dist/generate.js:235:31)
at async Command. (/go/src/github.com/influxdata/ui/node_modules/@influxdata/oats/bin/oats:7:18)
(node:47346) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:47346) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```

**oats/operationID.yaml**
```yaml
openapi: "3.0.0"
info:
title: Stuff service
version: 0.0.0
servers:
- url: /api/v2/stuff
paths:
/:
post:
operationId: createStuff
requestBody:
description: Stuff to create
content:
application/json:
schema:
$ref: "#/components/schemas/StuffCreate"
responses:
'204':
$ref: "#/components/responses/NoContent"
delete:
operationId: deleteStuff
summary: Delete stuff
parameters:
- $ref: "#/components/parameters/StuffDelete"
responses:
'204':
$ref: "#/components/responses/NoContent"
/things:
put:
operationId: updateThings
summary: Update a stuff's things
requestBody:
description: Thing to update
content:
application/json:
schema:
$ref: "#/components/schemas/Thing"
responses:
'204':
$ref: "#/components/responses/NoContent"
components:
parameters:
StuffDelete:
in: query
name: stuffDelete
required: false
description: stuff to delete
schema:
type: string
schemas:
StuffCreate:
type: object
properties:
stuff:
type: string
StuffDelete:
type: object
properties:
stuff:
type: string
Thing:
type: object
properties:
stuff:
type: string
responses:
NoContent:
description: No content
```

The current way to work around this is to remove the basename of the path, in this case `stuff`, and prepend it to every path defined in the swagger document.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with oats/operationID.yaml using the documented yarn run oats command and inspect the generated operationID.ts. Trace how operationId, the HTTP method, and the path become function names. Done means the createStuff and deleteStuff operations generate valid TypeScript names without requiring the path basename workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.