полезные нагрузки ответа go-swagger, а не типы моделей

Я занимаюсь дерзким видом мастера https://github.com/go-swagger/go-swagger/commit/3981236c3f6bd9eabb26f14e9d31b853d340405f

Не уверен, что это проблема или просто непонимание с моей стороны. У меня есть метод проверки работоспособности, который по умолчанию возвращает статус 500 вместе с объектом errorMessage, определенным в модели. Однако метод WithPayload принимает тип GetHealthcheckDefaultBodyBody. Здесь не такая большая проблема, поскольку я могу просто создать один из них, но там, где мне нужно возвращать массивы типов моделей, я в конечном итоге напишу много шаблонов для сопоставления типов моделей с типами BodyBody, например.

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.

Моя спецификация:

{
  "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

person Myles McDonnell    schedule 20.07.2016    source источник


Ответы (1)


Вы можете попробовать:

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

Ссылка гарантирует, что тот же тип будет повторно использован вместо создания нового из-за анонимного определения.

person Casual Jim    schedule 09.10.2016