amazon-web-services - AWS API 网关 : Documentation Swagger export model type null ignored

标签 amazon-web-services swagger aws-api-gateway jsonschema

这个问题在这里已经有了答案:





How to define a property that can be string or null in OpenAPI (Swagger)?

(1 个回答)


2年前关闭。




我在将 AWS 的 API 网关文档导出到 Swagger 时遇到问题。
我有一个包含空类型的 JSON 模式模型的文档部分,例如:

{
  "title": "Incident",
  "type": "object",
  "required": [
    "id"
  ],
  "properties": {
    "id": {
        "type": "string"
    },
    "createdAt": {
        "type": "string"
    },
    "updatedAt": {
        "type": ["string", "null"]
    }
  }
}

它适用于验证,它允许我为 updatedAt 字段发送空值。但是,在这种情况下,当我将文档导出到 Swagger 时,我只有这是我的 swagger 文件:
{
  "title": "Incident",
  "type": "object",
  "required": [
    "id"
  ],
  "properties": {
    "id": {
        "type": "string"
    },
    "createdAt": {
        "type": "string"
    }
  }
}

我的 updatedAt 字段被简单地忽略了,如果我设置了,我也会遇到同样的问题
"type": "null"

这确实有问题,因为我需要能够发送空值,在这种情况下,我的 swagger 文档缺少模型中的某些字段。

有什么方法可以在 JSON Schema 中定义一个不会被 AWS API Gateway 忽略但仍然允许我发送空值的类型?

谢谢您的帮助。

[编辑]

原来“可空”:真正的解决方案不适合这种情况。
API Gateway 似乎使用 OAI V2 并且不支持可空。
然而,当我们将模型导出到 Swagger(在本例中使用 OAI V3)时,我们会丢失 null 类型。总之,我需要找到一个适用于 OAI V2 和 V3 的空类型解决方案。

最佳答案

虽然 JSON Schema 支持 null 类型,但 OpenAPI 规范(以前的 Swagger)不支持。

Primitive data types in the OAS are based on the types supported by the JSON Schema Specification Wright Draft 00. Note that integer as a type is also supported and is defined as a JSON number without a fraction or exponent part. null is not supported as a type (see nullable for an alternative solution). Models are defined using the Schema Object, which is an extended subset of JSON Schema Specification Wright Draft 00.



https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types

相反,如果您希望可以为空,您可以使用 "nullable": true 的附加属性。 ,根据 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-20

OpenAPI 定义的“Schema Object”是 JSON Schema 的子超集。

关于amazon-web-services - AWS API 网关 : Documentation Swagger export model type null ignored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53592399/

相关文章:

django - 无法将aws RDS连接到django本地

python - JWT 不接受 Bearer 授权

java - 为什么 Maven swagger 插件不扫描外部模块?

amazon-web-services - 如何获取其他地区的ssm参数?

amazon-web-services - AWS HTTP API网关Lambda处理程序的 typescript

amazon-web-services - 将 API 网关主体中的 JSON 对象添加到 Kinesis 代理?

python - AWS ECS - 使用 Boto3 更新任务定义

ios - API 网关 : Empty request body?

amazon-web-services - 使用 InstanceProfileCredentialsProvider 是否是从 Ec2 实例连接到 AWS S3 的正确方法?

java - Swagger API 控制台无法在 WSO2 中工作