我不太确定问题出在哪里,但 FastAPI 生成了一个 Swagger 文件,我认为这是正确的,并且最新版本中的 openapi-generator-cli 认为 Swagger 文件无效。错误信息是:
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
| Error count: 1, Warning count: 2
Errors:
-attribute paths.'/units/{id}/charts/'(get).parameters.[group_mode].schemas.required is not of type `array`
Warnings:
-attribute paths.'/units/{id}/charts/'(get).parameters.[group_mode].schemas.required is not of type `array`
at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:701)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:728)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:519)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
这就是 swagger 参数的样子:
这就是 FastAPI 代码中的样子:
group_mode: GroupMode = Query(
default="1d",
description="Group by a parameter. Options are Minute, Hour, Day, Week, Month, Year.",
required=True,
),
第一个问题是,问题出在哪里。 swagger 是否正确,代码生成器的设置(客户端是 typescript-axios,但我尝试过其他人,同样的问题)是否错误?或者代码生成器实际上是正确的,而 FastAPI 生成的规范是错误的?还是我这边的编码问题?
required
必须是一个数组,并且必须至少指示架构中定义的属性之一。从我从你的例子中收集到的信息来看,你期待一个像 /units/{id}/charts?group_mode=week
这样的 url,它的定义正确,如下。
openapi: 3.0.3
info:
name: test
version: '1.0.0'
paths:
'/units/{id}/charts':
get:
description: blah
parameters:
- $ref: '#/components/parameters/group_mode'
- $ref: '#/components/parameters/id'
responses:
'200':
description: OK
content:
'application/json':
schema: {}
components:
schemas:
GroupMode:
schema:
type: string
enum:
- Hour
- Minute
- Day
- Week
- Month
- Year
parameters:
group_mode:
in: query
required: false
schema:
- $ref: '#/components/schemas/GroupMode'
description: your description
id:
in: path
required: true
schema:
type: string