From f4945676fdee130beedb370a7ea1901c5f1c9d6f Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Wed, 28 Mar 2018 16:21:57 -0700 Subject: [PATCH] The final swagger.yml and the built files that go with it. --- models/error_response.go | 2 +- restapi/embedded_spec.go | 16 +++----- restapi/operations/clock_get_parameters.go | 43 +++++++++++++--------- restapi/operations/clock_get_urlbuilder.go | 16 ++++++++ swagger.yml | 9 ++--- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/models/error_response.go b/models/error_response.go index a9e99ae..6fbd71f 100644 --- a/models/error_response.go +++ b/models/error_response.go @@ -18,7 +18,7 @@ import ( type ErrorResponse struct { // code - Code int64 `json:"code,omitempty"` + Code int32 `json:"code,omitempty"` // message // Required: true diff --git a/restapi/embedded_spec.go b/restapi/embedded_spec.go index 8ba1f64..8f59423 100644 --- a/restapi/embedded_spec.go +++ b/restapi/embedded_spec.go @@ -49,12 +49,10 @@ func init() { "operationId": "ClockGet", "parameters": [ { + "type": "string", "description": "Timezone to return", "name": "timezone", - "in": "body", - "schema": { - "$ref": "#/definitions/timezone" - } + "in": "query" } ], "responses": { @@ -123,7 +121,7 @@ func init() { "properties": { "code": { "type": "integer", - "format": "int64" + "format": "int32" }, "message": { "type": "string" @@ -185,12 +183,10 @@ func init() { "operationId": "ClockGet", "parameters": [ { + "type": "string", "description": "Timezone to return", "name": "timezone", - "in": "body", - "schema": { - "$ref": "#/definitions/timezone" - } + "in": "query" } ], "responses": { @@ -259,7 +255,7 @@ func init() { "properties": { "code": { "type": "integer", - "format": "int64" + "format": "int32" }, "message": { "type": "string" diff --git a/restapi/operations/clock_get_parameters.go b/restapi/operations/clock_get_parameters.go index f1438bc..07d2c80 100644 --- a/restapi/operations/clock_get_parameters.go +++ b/restapi/operations/clock_get_parameters.go @@ -12,7 +12,7 @@ import ( "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" - models "github.com/elfsternberg/timeofday/models" + strfmt "github.com/go-openapi/strfmt" ) // NewClockGetParams creates a new ClockGetParams object @@ -32,9 +32,9 @@ type ClockGetParams struct { HTTPRequest *http.Request `json:"-"` /*Timezone to return - In: body + In: query */ - Timezone *models.Timezone + Timezone *string } // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface @@ -46,25 +46,32 @@ func (o *ClockGetParams) BindRequest(r *http.Request, route *middleware.MatchedR o.HTTPRequest = r - if runtime.HasBody(r) { - defer r.Body.Close() - var body models.Timezone - if err := route.Consumer.Consume(r.Body, &body); err != nil { - res = append(res, errors.NewParseError("timezone", "body", "", err)) - } else { + qs := runtime.Values(r.URL.Query()) - // validate body object - if err := body.Validate(route.Formats); err != nil { - res = append(res, err) - } - - if len(res) == 0 { - o.Timezone = &body - } - } + qTimezone, qhkTimezone, _ := qs.GetOK("timezone") + if err := o.bindTimezone(qTimezone, qhkTimezone, route.Formats); err != nil { + res = append(res, err) } + if len(res) > 0 { return errors.CompositeValidationError(res...) } return nil } + +func (o *ClockGetParams) bindTimezone(rawData []string, hasKey bool, formats strfmt.Registry) error { + var raw string + if len(rawData) > 0 { + raw = rawData[len(rawData)-1] + } + + // Required: false + // AllowEmptyValue: false + if raw == "" { // empty values pass all other validations + return nil + } + + o.Timezone = &raw + + return nil +} diff --git a/restapi/operations/clock_get_urlbuilder.go b/restapi/operations/clock_get_urlbuilder.go index b1d95be..b7b6320 100644 --- a/restapi/operations/clock_get_urlbuilder.go +++ b/restapi/operations/clock_get_urlbuilder.go @@ -13,7 +13,11 @@ import ( // ClockGetURL generates an URL for the clock get operation type ClockGetURL struct { + Timezone *string + _basePath string + // avoid unkeyed usage + _ struct{} } // WithBasePath sets the base path for this url builder, only required when it's different from the @@ -43,6 +47,18 @@ func (o *ClockGetURL) Build() (*url.URL, error) { } result.Path = golangswaggerpaths.Join(_basePath, _path) + qs := make(url.Values) + + var timezone string + if o.Timezone != nil { + timezone = *o.Timezone + } + if timezone != "" { + qs.Set("timezone", timezone) + } + + result.RawQuery = qs.Encode() + return &result, nil } diff --git a/swagger.yml b/swagger.yml index 54e947a..9e80363 100644 --- a/swagger.yml +++ b/swagger.yml @@ -44,7 +44,7 @@ definitions: properties: code: type: integer - format: int64 + format: int32 message: type: string required: @@ -80,12 +80,11 @@ paths: description: "Returns time of day." operationId: "ClockGet" parameters: - - in: "body" - name: "timezone" + - in: query + name: timezone description: "Timezone to return" required: false - schema: - $ref: "#/definitions/timezone" + type: string responses: 200: description: "Returns the time of day."