我正在尝试将一个大的YML分成一堆较小的YML文档。我遵循了大卫·加西亚(David Garcia)提供的示例在这里< /a>,然后使用OpenAPI CodeGenerator生成我的模型。 OpenAPI Generator抱怨 [BUG]属性组件.schemas.schema名称不遵守正则表达式 ^[A-ZA-Z0-9 .-_]+$
。因此,我试图通过克隆他的回购并在当地部署他的榜样与David Garcia的榜样一起玩,但我遇到了同样的错误。 https://editor.swagger.io/”中检查它。
Semantic error at components.schemas.$ref
Component names can only contain the characters A-Z a-z 0-9 - . _
Jump to line 25
我决定在
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: Multi-file boilerplate for OpenAPI Specification.
license:
name: MIT
contact:
name: API Support
url: http://www.example.com/support
email: [email protected]
servers:
- url: http://petstore.swagger.io/v1
tags:
- name: pets
paths:
/pets:
$ref: "./resources/pets.yaml"
/pets/{petId}:
$ref: "./resources/pet.yaml"
components:
parameters:
$ref: "./parameters/_index.yaml"
schemas:
$ref: "./schemas/_index.yaml"
responses:
$ref: "./_index.yaml"
您可以轻松地将其粘贴到编辑器并亲自查看错误。 OpenAPI规范说 components既可以是对象对象或参考,IE 映射[字符串,架构对象|参考对象]
,和:“或者,每当可以使用架构对象时,可以在其位置使用参考
对象
components:
parameters:
petId:
$ref: "./parameters/path/petId.yaml"
schemas:
pets:
$ref: "./schemas/Pets.yaml"
responses:
responseSchema:
$ref: "./response/pets200.yaml"
。为什么我不能引用外部索引? 在线示例
I'm trying to split a large yml into a bunch of smaller yml documents. I followed the example provided by David Garcia here, and then using OpenAPI CodeGenerator to generate my models. OpenAPI Generator complained that [BUG] attribute components.schemas.Schema name doesn't adhere to regular expression ^[a-zA-Z0-9.-_]+$
. So, I tried playing with David Garcia's example by cloning his repo and deploying locally, but I get the same error. I decided to check it in the swagger editor, and I get the same issue, but the error message says
Semantic error at components.schemas.$ref
Component names can only contain the characters A-Z a-z 0-9 - . _
Jump to line 25
I'm using the yaml from David Garcia's example:
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: Multi-file boilerplate for OpenAPI Specification.
license:
name: MIT
contact:
name: API Support
url: http://www.example.com/support
email: [email protected]
servers:
- url: http://petstore.swagger.io/v1
tags:
- name: pets
paths:
/pets:
$ref: "./resources/pets.yaml"
/pets/{petId}:
$ref: "./resources/pet.yaml"
components:
parameters:
$ref: "./parameters/_index.yaml"
schemas:
$ref: "./schemas/_index.yaml"
responses:
$ref: "./_index.yaml"
You can easily paste this into the editor and see the errors yourself. The OpenAPI Specification says components objects can either be an Object or a Reference, i.e. Map[string, Schema Object | Reference Object]
, and the Schema Object definition says, "Alternatively, any time a Schema Object can be used, a Reference Object can be used in its place. "
I'm aware that I can sub it down within the yaml document, like so:
components:
parameters:
petId:
$ref: "./parameters/path/petId.yaml"
schemas:
pets:
$ref: "./schemas/Pets.yaml"
responses:
responseSchema:
$ref: "./response/pets200.yaml"
But why can't I reference an external index? The online example says yes and the open api spec says yes, but I can't get it to work.
发布评论
评论(1)
这是一个普遍的误解,即OpenAPI规范允许
$ ref
whery 。实际上,$ ref
仅在OpenAPI规范说字段的值可以是“参考对象”或“架构对象”的地方允许。具体来说,此片段不是有效的OpenAPI语法:
MAP [String,架构对象|参考对象]
表示components.schemas
节点必须是一个映射,其中键是架构名称,并且值是内联架构或架构引用。如您的第二个示例(有效的OpenAPI语法):某些实现用于处理$ refs的解决方案的方法是使用通用JSON $ REF RESOLVER预先处理规格(例如 json-refs )来解决这些非标准$ refs。例如,博客文章您从使用
Swagger-Cli
中获取了此示例来解决非标准$ refs并创建一个合并的文件。It's a common misconception that the OpenAPI Specification allows
$ref
anywhere. Actually,$ref
is only allowed in places where the OpenAPI Specification says the value of a field can be a "Reference Object" or a "Schema Object".Specifically, this snippet is NOT valid OpenAPI syntax:
Map[string, Schema Object | Reference Object]
means that thecomponents.schemas
node must be a map where the keys are schema names and the values are either inline schemas or schema references. As in your second example (which is valid OpenAPI syntax):The workaround that some implementations use to handle $refs in any places is to pre-process the spec using a generic JSON $ref resolver (such as json-refs) to resolve those non-standard $refs. For example, the blog post you took this example from uses
swagger-cli
to resolve non-standard $refs and create a single merged file.