AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

    • 主页
    • 系统&网络
    • Ubuntu
    • Unix
    • DBA
    • Computer
    • Coding
    • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[openapi](coding)

Martin Hope
Luis Abreu
Asked: 2025-02-05 00:55:49 +0800 CST

OpenAPI 规范和 URL 规范化

  • 6

快速提问:OpenAPI 规范是否要求在将当前 URL 与路径匹配之前对验证器进行 URL 规范化?再一次,我发现正在使用的验证器似乎有缺陷,因为它会将规则应用于 /abc/path,但不应用于 /abc/path/(请注意额外的 /)。

谢谢。

openapi
  • 1 个回答
  • 18 Views
Martin Hope
Dalvine
Asked: 2024-10-16 21:38:01 +0800 CST

在 OpenAPI 响应模式中使用 oneOf

  • 5

堆

  • 快

数据

  • 使用 addSchema 方法添加了几个架构:
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    
})
  • GET 端点的架构:
{
    "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 会在服务器的构建和启动阶段抛出错误,但在这种情况下不会这样做。

openapi
  • 1 个回答
  • 19 Views
Martin Hope
SamDroid
Asked: 2024-08-02 20:23:28 +0800 CST

Fastify Swagger 未按预期管理枚举器

  • 7

我们正在使用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]

否则,各个包将无法正确呈现使用枚举的地方。

你已经遇到过这个问题了吗?

openapi
  • 1 个回答
  • 18 Views
Martin Hope
JDev
Asked: 2024-07-25 13:56:48 +0800 CST

Swagger UI 和 OpenAPI 3 - 通过示例 $ref 切换请求主体示例

  • 5

我有 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": { .... }
      }
    }
  }
openapi
  • 2 个回答
  • 34 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助
subwaysurfers
my femboy roommate

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve