OpenAPI(fka.Swagger)规范可以通过使用x-
属性进行扩展(参见规范扩展是规范)。标准 OpenAPI 解析器会忽略这些x-
属性。
即使在 JSON 模式定义中,您几乎可以在规范文件的任何位置定义自己的属性,但是您必须编写自己的解析器才能使用它们和/或修改 Swagger UI 等工具(这很容易做到)才能看到他们在这样的工具中。
这是一个在定义中包含一些 x 张力的示例:
swagger: "2.0"
info:
version: 1.0.0
title: X-tension example
description: Using x- properties to extend the OpenAPI specification
x-example: we can put x-tension almost anywhere in the specification
paths: {}
definitions:
User:
properties:
id:
type: string
name:
type: string
maxLength: 50
minLength: 10
x-validation:
multiLingualMessage:
en: Name's length must be between <minLength> and <maxLength>
fr: La longeur de Name doit être comprise entre <minLength> et <maxLength>
friends:
type: array
description: An array of UserId
items:
type: string
x-reference:
type: User
这个 OpenAPI 规范被编辑器认为是有效的:它忽略了x-
属性。
此示例仅是x-
属性的说明,并不打算解决问题中列出的所有用例。