快速提问:OpenAPI 规范是否要求在将当前 URL 与路径匹配之前对验证器进行 URL 规范化?再一次,我发现正在使用的验证器似乎有缺陷,因为它会将规则应用于 /abc/path,但不应用于 /abc/path/(请注意额外的 /)。
谢谢。
快速提问:OpenAPI 规范是否要求在将当前 URL 与路径匹配之前对验证器进行 URL 规范化?再一次,我发现正在使用的验证器似乎有缺陷,因为它会将规则应用于 /abc/path,但不应用于 /abc/path/(请注意额外的 /)。
谢谢。
堆
数据
server.addSchema({
$id: 'event',
type: 'object',
properties: {
event_id: {
$ref: 'id'
},
address: {
$ref: 'address'
},
starttime: {
$ref: 'time'
},
name: {
type: 'string'
}
},
required: ['event_id', 'address', 'starttime']
})
server.addSchema({
$id: 'callEvent',
type: 'object',
allOf: [{ $ref: 'event' }],
properties: {
type: {
type: 'string',
const: 'call'
},
num: {
type: 'integer'
},
result: {
type: 'integer',
minimum: 0,
maximum: 3
}
},
required: ['type', 'num', 'result'],
unevaluatedProperties: false
})
server.addSchema({
$id: 'appEvent',
type: 'object',
allOf: [{ $ref: 'event' }],
properties: {
type: {
type: 'string',
const: 'app'
}
},
required: ['type'],
unevaluatedProperties: false
})
{
"response": {
"200": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "object",
"oneOf": [
{ "$ref": "appEvent" },
{ "$ref": "callEvent" }
]
}
}
}
}
}
我的目标:我需要指定响应返回一个对象,其中的键可以是任意值,并且属性是一个数组,其中的元素可以匹配任何指定的模式。
预期有效结果:
{
"otherkey": [
{
"event_id": 1,
"address": "string",
"starttime": " 00:00",
"name": "string",
"type": "app" // this scheme appEvent
},
{
"event_id": 2,
"address": "string",
"starttime": " 06:08",
"name": "string",
"type": "call",
"num": 0,
"result": 3 // this scheme callEvent
}
],
"otherkey2": [
{
"event_id": 1,
"address": "string",
"starttime": " 00:00",
"name": "string",
"type": "app" // this scheme appEvent
}
],
"otherkey3": [
{
"event_id": 2,
"address": "string",
"starttime": " 06:08",
"name": "string",
"type": "call",
"num": 0,
"result": 3 // this scheme callEvent
}
]
}
实际结果:
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "The value of '#/additionalProperties/items' does not match schema definition."
}
如果我做错了什么,请纠正我。但根据架构https://json-schema.org/draft-07/schema,这应该可以正常工作。
注意:我也在我的堆栈中使用@fastify/swagger
和@fastify/swagger-ui
。在生成的 Swagger 文档中,此端点的示例按我的预期显示。
Swagger 中的示例如下:
{
"additionalProp1": [
{
"event_id": 1,
"address": "string",
"starttime": "23:07",
"name": "string",
"type": "string"
},
{
"event_id": 1,
"address": "string",
"starttime": "22:40",
"name": "string",
"type": "string",
"num": 0,
"result": 3
}
],
"additionalProp2": [
{
"event_id": 1,
"address": "string",
"starttime": "23:07",
"name": "string",
"type": "string"
},
{
"event_id": 1,
"address": "string",
"starttime": "22:40",
"name": "string",
"type": "string",
"num": 0,
"result": 3
}
],
"additionalProp3": [
{
"event_id": 1,
"address": "string",
"starttime": "23:07",
"name": "string",
"type": "string"
},
{
"event_id": 1,
"address": "string",
"starttime": "22:40",
"name": "string",
"type": "string",
"num": 0,
"result": 3
}
]
}
我曾尝试搜索有关此主题的不同信息,但所有示例都显示 oneOf 和 anyOf 的用法与我使用的方式相同。
通常,如果模式中存在无效键或模式不正确,Fastify 会在服务器的构建和启动阶段抛出错误,但在这种情况下不会这样做。
我们正在使用Fastify开发后端,使用Typebox声明模式并使用Fastify Swagger生成 OpenApi 定义 我们在生成文档期间遇到了一些枚举或类似问题
假设我们以下列方式之一声明一个类型框枚举器
export const ProviderType = Type.Union([
Type.Literal("sms"),
Type.Literal("email"),
]);
//OR
enum Providers {
SMS = "sms",
EMAIL = "email",
}
export const ProviderType = Type.Enum(Providers);
当生成开放 API 文档时,我们得到这个
- schema:
anyOf:
- type: string
enum:
- sms
- type: string
enum:
- email
这不是我们所期望的,而是
schema:
type: string
enum: [sms, email]
否则,各个包将无法正确呈现使用枚举的地方。
你已经遇到过这个问题了吗?
我有 2 个架构和一个示例。我想使用下拉菜单更改请求主体。
实际上一切都正常。我有一个显示示例的列表,并且仅当我将示例写为“value”时,RequestBody 中的值才会更改。如果我将示例指定为 ref,则会加载第一个示例,但当下拉列表中的值发生变化时不会显示第二个示例。
这难道只是没有实现,这是一个错误还是我做错了什么/配置了什么?
配置示例:
"requestBody": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{"$ref": "#/components/schemas/reportexample1"},
{"$ref": "#/components/schemas/reportexample2"}
]
},
"examples": {
"reportexample1": {
"summary": "reportexample1",
"$ref": "#/components/schemas/reportexample1"
},
"reportexample2": {
"summary": "reportexample2",
"$ref": "#/components/schemas/reportexample2"
}
}
}
}
},
......
"components": {
"schemas": {
"reportexample1": {
"type": "object",
"properties": { .... }
},
"reportexample2": {
"type": "object",
"properties": { .... }
}
}
}