求助:为Node API的Swagger文档编写带类型的响应Schema
搞定你的Swagger响应定义
我来帮你把这个GeoJSON格式的响应转换成准确的Swagger Schema定义。首先咱们先拆解你给出的JSON结构:
- 最外层是一个数组,里面包含一个
FeatureCollection对象 FeatureCollection里有一个features数组,每个元素是Feature对象Feature包含geometry(点类型)和properties(业务数据)两个部分
接下来把你的Swagger响应代码修改成对应的结构,每个嵌套对象都明确类型定义:
responses: 200: description: Successful response schema: type: array items: title: FeatureCollection type: object properties: type: type: string example: "FeatureCollection" features: type: array items: title: Feature type: object properties: type: type: string example: "Feature" geometry: title: PointGeometry type: object properties: type: type: string example: "Point" coordinates: type: array items: type: number example: [-79.90295559, 36.05593626] properties: title: FeatureProperties type: object properties: id: type: string example: "f2e08a02-9be9-468d-9bae-b83118ef5110" esri_id: type: string example: "518" name: type: string example: "Greensboro et al, NC" panelCount: type: integer example: 1 tot: type: integer example: 790523 required: - type - geometry - properties required: - type - features
关键点说明:
- 最外层的
type: array对应你JSON的顶层数组结构 - 每个嵌套对象都通过
type: object定义,并列出对应的properties,明确每个字段的类型(string、integer、number等) - 添加了
example字段可以让Swagger文档更直观,展示每个字段的示例值 required数组用来标记那些必须存在的字段,你可以根据实际业务需求调整是否需要保留
这样定义后,Swagger文档就能准确展示你API返回的GeoJSON结构了。
内容的提问来源于stack exchange,提问作者LogaKrishnan
相关产品推荐
相关产品推荐

