首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >go-swagger响应有效负载不是模型类型

go-swagger响应有效负载不是模型类型
EN

Stack Overflow用户
提问于 2016-07-20 18:28:55
回答 1查看 964关注 0票数 1

我在运行go-swagger head of master https://github.com/go-swagger/go-swagger/commit/3981236c3f6bd9eabb26f14e9d31b853d340405f

我不确定这是一个问题,还是我缺乏理解。我有一个健康检查方法,默认情况下返回一个状态500,以及一个在模型中定义的errorMessage对象。但是,WithPayload方法采用GetHealthcheckDefaultBodyBody类型。这不是什么大问题,因为我可以创建其中的一个,但是在我需要返回模型类型数组的地方,我最终会编写很多样板文件来从模型类型映射到BodyBody类型,例如。

代码语言:javascript
复制
func ClientsGet(params clients.GetClientsParams) middleware.Responder {

    results, err := repository.GetAllClients()

    if err != nil {
        return clients.NewGetClientsDefault(500).WithPayload(clients.GetClientsDefaultBodyBody{Message: sPtr(err.Error())})
    }


    return &clients.GetClientsOK{results} //does not compile as GetClientsOK accepts []*clients.GetClientsOKBodyBody not []*models.Client
}

我注意到代码库中的示例代码在这方面与我生成的代码不同:https://github.com/go-swagger/go-swagger/blob/3981236c3f6bd9eabb26f14e9d31b853d340405f/examples/tutorials/todo-list/server-complete/restapi/operations/todos/add_one_responses.go。我无法从这里的示例规范生成代码:https://github.com/go-swagger/go-swagger/blob/3981236c3f6bd9eabb26f14e9d31b853d340405f/examples/tutorials/todo-list/server-complete/swagger.yml

我的规格:

代码语言:javascript
复制
{
  "swagger": "2.0",
  "info": {
    "title": "M3 Intl Maas Service",
    "version": "0.1.0"
  },
  "produces": [
    "application/json"
  ],
  "consumes": [
    "application/json"
  ],
  "schemes": [
    "http"
  ],
  "definitions": {
    "client": {
      "properties": {
        "id": {
          "format": "int64",
          "type": "integer"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "name"
      ],
      "type": "object"
    },
    "messageTeaser": {
      "properties": {
        "campaignId": {
          "type": "string"
        },
        "clientName": {
          "type": "string"
        },
        "footer": {
          "type": "string"
        },
        "id": {
          "type": "string"
        },
        "isNew": {
          "type": "boolean"
        },
        "jobNumber": {
          "type": "string"
        },
        "piLink": {
          "type": "string"
        },
        "repId": {
          "type": "string"
        },
        "summary": {
          "type": "string"
        },
        "title": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "title",
        "summary",
        "isNew",
        "footer",
        "jobNumber",
        "clientName",
        "piLink",
        "repId",
        "campaignId"
      ],
      "type": "object"
    },
    "errorMessage": {
      "type": "object",
      "required": [
        "message"
      ],
      "properties": {
        "message": {
          "type": "string"
        }
      }
    }
  },
  "paths": {
    "/clients": {
      "get": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          }
        ],
        "responses": {
          "200": {
            "description": "get client",
            "schema": {
              "items": {
                "properties": {
                  "id": {
                    "format": "int64",
                    "type": "integer"
                  },
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "name"
                ],
                "type": "object"
              }
            }
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "type": "object",
              "required": [
                "message"
              ],
              "properties": {
                "message": {
                  "type": "string"
                }
              }
            }
          }
        },
        "tags": [
          "clients"
        ]
      },
      "put": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          },
          {
            "in": "body",
            "required": true,
            "name": "client",
            "schema": {
              "properties": {
                "id": {
                  "format": "int64",
                  "type": "integer"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "name"
              ],
              "type": "object"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "update client",
            "schema": {
              "items": {
                "properties": {
                  "id": {
                    "format": "int64",
                    "type": "integer"
                  },
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "name"
                ],
                "type": "object"
              }
            }
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "type": "object",
              "required": [
                "message"
              ],
              "properties": {
                "message": {
                  "type": "string"
                }
              }
            }
          }
        },
        "tags": [
          "clients"
        ]
      },
      "delete": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          }
        ],
        "responses": {
          "200": {
            "description": "delete client"
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "type": "object",
              "required": [
                "message"
              ],
              "properties": {
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "/clients/{client_id}": {
      "get": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          }
        ],
        "responses": {
          "200": {
            "description": "get client",
            "schema": {
              "items": {
                "properties": {
                  "id": {
                    "format": "int64",
                    "type": "integer"
                  },
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "name"
                ],
                "type": "object"
              }
            }
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "items": {
                "type": "object",
                "required": [
                  "message"
                ],
                "properties": {
                  "message": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "tags": [
          "clients"
        ]
      },
      "put": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          },
          {
            "in": "body",
            "required": true,
            "name": "client",
            "schema": {
              "properties": {
                "id": {
                  "format": "int64",
                  "type": "integer"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "name"
              ],
              "type": "object"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "update client",
            "schema": {
              "items": {
                "properties": {
                  "id": {
                    "format": "int64",
                    "type": "integer"
                  },
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "name"
                ],
                "type": "object"
              }
            }
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "items": {
                "type": "object",
                "required": [
                  "message"
                ],
                "properties": {
                  "message": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "tags": [
          "clients"
        ]
      },
      "delete": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "client_id",
            "type": "integer"
          }
        ],
        "responses": {
          "200": {
            "description": "delete client"
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "items": {
                "type": "object",
                "required": [
                  "message"
                ],
                "properties": {
                  "message": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/healthcheck": {
      "get": {
        "responses": {
          "200": {
            "description": "confirm that the service is healthy"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "type": "object",
              "required": [
                "message"
              ],
              "properties": {
                "message": {
                  "type": "string"
                }
              }
            }
          }
        },
        "tags": [
          "healthcheck"
        ]
      }
    },
    "/usermessagesummary/{community_id}/{user_id}/{lang_id}": {
      "get": {
        "parameters": [
          {
            "in": "path",
            "required": true,
            "name": "community_id",
            "type": "string"
          },
          {
            "in": "path",
            "required": true,
            "name": "user_id",
            "type": "string"
          },
          {
            "in": "path",
            "required": true,
            "name": "lang_id",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "Returns message summary according to criteria",
            "schema": {
              "items": {
                "properties": {
                  "campaignId": {
                    "type": "string"
                  },
                  "clientName": {
                    "type": "string"
                  },
                  "footer": {
                    "type": "string"
                  },
                  "id": {
                    "type": "string"
                  },
                  "isNew": {
                    "type": "boolean"
                  },
                  "jobNumber": {
                    "type": "string"
                  },
                  "piLink": {
                    "type": "string"
                  },
                  "repId": {
                    "type": "string"
                  },
                  "summary": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "title",
                  "summary",
                  "isNew",
                  "footer",
                  "jobNumber",
                  "clientName",
                  "piLink",
                  "repId",
                  "campaignId"
                ],
                "type": "object"
              }
            }
          },
          "401": {
            "description": "unauthorised"
          },
          "404": {
            "description": "client not found"
          },
          "default": {
            "description": "generic error response",
            "schema": {
              "items": {
                "type": "object",
                "required": [
                  "message"
                ],
                "properties": {
                  "message": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-09 15:32:53

您可以尝试使用:

"200": { "description": "get client", "schema": { "items": { "$ref": "#/definitions/client" } }

ref确保相同的类型被重用,而不是因为匿名定义而生成新类型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38478783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档