我正在为其编写 Swagger 2.0 规范的 API 基本上是任何 JSON 值的存储。
我想要一个读取值的路径和一个存储非预定义深度的任何 JSON 值(空、数字、整数、字符串、对象、数组)的路径。
不幸的是,Swagger 2.0 对输入和输出的模式似乎相当严格,并且不允许 JSON Schema 允许的整套模式。 Swagger 编辑器不允许使用混合值(例如可以是布尔值或整数的属性)或松散定义的数组(必须严格定义项目的类型)和对象。
所以我正在尝试通过定义一个解决方法MixedValue
schema:
---
swagger: '2.0'
info:
version: 0.0.1
title: Data store API
consumes:
- application/json
produces:
- application/json
paths:
/attributes/{attrId}/value:
parameters:
- name: attrId
in: path
type: string
required: true
get:
responses:
'200':
description: Successful.
schema:
$ref: '#/definitions/MixedValue'
put:
parameters:
- name: value
in: body
required: true
schema:
$ref: '#/definitions/MixedValue'
responses:
responses:
'201':
description: Successful.
definitions:
MixedValue:
type: object
properties:
type:
type: string
enum:
- 'null'
- boolean
- number
- integer
- string
- object
- array
boolean:
type: boolean
number:
type: number
integer:
type: integer
string:
type: string
object:
description: deep JSON object
type: object
additionalProperties: true
array:
description: deep JSON array
type: array
required:
- type
但 Swagger 编辑器拒绝松散定义object
and array
特性。
问题:
- 有没有办法解决这个问题?
- 这只是 Swagger 编辑器错误还是 Swagger 2.0 规范的严格限制?
- 有没有更好的方法(最佳实践)来指定我需要什么?
- 我可以预期 swagger 为某些具有我的 API 规范的语言生成的代码会受到一些限制吗?
可以使用空模式定义任意类型的模式{}
:
# swagger: '2.0'
definitions:
AnyValue: {}
# openapi: 3.0.0
components:
schemas:
AnyValue: {}
或者如果你想要一个description
:
# swagger: '2.0'
definitions:
AnyValue:
description: 'Can be anything: string, number, array, object, etc. (except `null`)'
# openapi: 3.0.0
components:
schemas:
AnyValue:
description: 'Can be anything: string, number, array, object, etc., including `null`'
没有定义的type
,模式允许任何值。请注意,OpenAPI 2.0 规范不支持null
值,但某些工具可能仍然支持空值。
在 OpenAPI 3.0 中,type
-less 模式允许null
值,除非其他约束(例如enum
).
See 本次问答有关如何操作的更多详细信息type
-less 模式有效。
就是这样招摇编辑器2.0处理主体参数AnyValue
schema:
我不知道代码生成器如何处理这个问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)