openapi2aspida icon indicating copy to clipboard operation
openapi2aspida copied to clipboard

components.requestBodiesの型が生成されない

Open MH4GF opened this issue 4 years ago • 0 comments

Description

表題通りです。

OpenAPI定義

openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample
paths:
  /users:
    post:
      summary: create user
      operationId: post-users
      responses:
        '200':
          description: OK
      description: ''
      requestBody:
        $ref: '#/components/requestBodies/UserRequestBody'
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
  requestBodies:
    UserRequestBody:
      content:
        application/json:
          schema:
            type: object
            properties:
              name:
                type: string
            required:
              - name

出力結果

# @types/index.ts

/* eslint-disable */
export type User = {
  id: string
  name: string
}

# users/index.ts

/* eslint-disable */
import type * as Types from '../@types'

export type Methods = {
  post: {
    status: 200
    reqBody: Types.UserRequestBody // 型への参照は生成されるが、参照先の型が存在しない。
  }
}

期待される結果

# @types/index.ts

/* eslint-disable */
export type User = {
  id: string
  name: string
}

// requestBodiesの型が生成されている
export type UserRequestBody = {
  name: string
}

# users/index.ts

/* eslint-disable */
import type * as Types from '../@types'

export type Methods = {
  post: {
    status: 200
    reqBody: Types.UserRequestBody
  }
}

Environment

  • Package version: vX.X.X
  • OS:
    • [ ] Linux
    • [ ] Windows
    • [x] macOS
  • Node.js version: v14.18.1
  • npm version: 6.14.15

Additional context

componentsの型生成はschemaとparametersしかやっていないようだったので、requestBodiesでも型生成を行うように修正してみています。 https://github.com/aspida/openapi2aspida/compare/master...MH4GF:feature/support-request-bodies 私のプロジェクトで必要だった型の生成はできており、PullRequestを上げようと思ったものの、以下の問題が起きており一旦上げずにいます。


samples/swagger/@types/index.ts で予期しない型の生成が起きてしまっています。 UserArray , Pet https://github.com/MH4GF/openapi2aspida/blob/1e39700440d7b0e9d91248aabb89e30b23d48c1a/samples/swagger/%40types/index.ts#L50-L52

swagger.yamlではrequestBodiesの定義はないのにも関わらずrequestBodiesとして判定されてしまっているようです。 軽くデバッグしてみたところ、 UserArray のdescriptionは List of user object で、yamlの中には二つ存在します。 https://github.com/aspida/openapi2aspida/blob/master/samples/swagger.yaml#L427 https://github.com/aspida/openapi2aspida/blob/master/samples/swagger.yaml#L451

このうち一つのdescriptionの内容を適当に編集するとrequestBodiesとして判定されることは無くなりました。 pathの中で、description, schemaが重複するものがある場合requestBodiesと判定されてしまうようです。 おそらく依存するswagger-parserのissueかと思われますが、関連するissueは見つけられず。何か知見をお持ちであればコメントをいただきたいです。 引き続き調査を続けてみるつもりです。

MH4GF avatar Nov 27 '21 11:11 MH4GF