You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:为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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:15:28