【问题标题】:Openapi schema incorrectOpenapi 架构不正确
【发布时间】:2021-05-25 13:10:09
【问题描述】:

我制定了一个 openapi 规范,我们用它来生成代码 BE 和 FE。它工作正常,但给我一个关于我的模式类型的警告。然后它默认为“对象”,这就是它起作用的原因,但这不是一个好的解决方案。

请查看代码并帮助我查看我在这里遗漏的一些(明显的)内容。谢谢。 错误:

[main] INFO  o.o.c.l.TypeScriptAngularClientCodegen - generating code for Angular 11.0.0 ...
[main] INFO  o.o.c.l.TypeScriptAngularClientCodegen -   (you can select the angular version by setting the additionalProperty ngVersion)
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] INFO  o.o.codegen.TemplateManager - writing file /home/chai/Nextcloud/Documents/nerdspul/werkspul/dev-division/mosar/src/frontend/mosar-frontend/./build/openapi/model/./flashcard.ts
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - #components/schemas/Flashcard is not defined
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - #components/schemas/Flashcard is not defined
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#components/schemas/Flashcard. Default to 'object'
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - #components/schemas/Flashcard is not defined
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#components/schemas/Flashcard. Default to 'object'
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - #components/schemas/Flashcard is not defined
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#components/schemas/Flashcard. Default to 'object'
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.utils.ModelUtils - #components/schemas/Flashcard is not defined
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard
[main] WARN  o.o.codegen.DefaultCodegen - Error obtaining the datatype from ref:#components/schemas/Flashcard. Default to 'object'
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: #components/schemas/Flashcard

还有代码:

openapi: '3.0.0'
servers:
  # - url: http://localhost:8080/api
  # mockapi.io
  - url: https://6071a9e850aaea0017284e9d.mockapi.io/api

info:
  version: 1.0.0
  title: Flashcard API
paths:
  /flashcard/{id}:
    get:
      tags:
        - FlashcardApi
      description: Returns a single flashcard by id.
      operationId: getFlashcard
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        200:
          $ref: '#/components/schemas/Flashcard'
        404:
          description: Flashcard does not exist.
        500:
          description: A server error occurred.
  /flashcard:
    post:
      tags:
        - FlashcardApi
      description: Saves a single flashcard in the db.
      operationId: createFlashcard
      requestBody:
        description: Json object to create a flashcard
        required: true
        content:
          application/json:
            schema:
              $ref: '#components/schemas/Flashcard'

      responses:
        200:
          description: A flashcard.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Flashcard'
        404:
          description: Flashcard does not exist.
        500:
          description: A server error occurred.

components:
  schemas:
    Flashcard:
      type: object
      properties:
        term:
          type: string
        explanation:
          type: string
        id:
          type: string
        createDate:
          type: string
        changeDate:
          type: string
        createdBy:
          type: string
        changedBy:
          type: string
      required:
        - term
        - explanation

【问题讨论】:

    标签: angular openapi openapi-generator


    【解决方案1】:

    更改:$ref: '#components/schemas/Flashcard' 收件人:$ref: '#/components/schemas/Flashcard'(你错过了 /)

    也将此添加到第一个 get (/flashcard/{id}:) 返回正确的类型:

     responses:
       200:
         description: flash card
           content:
             application/json:
               schema:
                 $ref: '#/components/schemas/Flashcard'
    

    【讨论】:

    • 哇,很简单,很容易被忽视。谢谢!
    • 我重写了你所拥有的但仍然没有注意到它,不得不逐行检查:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多