Skip to content

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

@glinton

Description

@glinton

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.

# 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<DeleteResult> =>
    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.<anonymous> (/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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions