Openapi 架构不正确

问题描述

我制定了一个 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

解决方法

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

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

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