Skip to content

Client Headers in response panic #2544

@kenjones-cisco

Description

@kenjones-cisco

Problem statement

When a response has a header like Location with a type string and format uri, and the value is provided it will trigger a panic on the client.
The latest template will still call strfmt.Parse and the type is always a pointer when UnmarshalText is used (https://github.com/go-openapi/strfmt/blob/master/format.go#L311).
As a result when the generated client code attempts to assert the type, it is now being asserted as a value and not a pointer. The type assertion is now causing the generated client to panic.

Currently generated code

func (o *AddResourceCreated) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	// hydrates response header Location
	hdrLocation := response.GetHeader("Location")

	if hdrLocation != "" {
		vallocation, err := formats.Parse("uri", hdrLocation)
		if err != nil {
			return errors.InvalidType("Location", "header", "strfmt.URI", hdrLocation)
		}
		o.Location = vallocation.(strfmt.URI)
	}

	// hydrates response header X-Request-Id
	hdrXRequestID := response.GetHeader("X-Request-Id")

	if hdrXRequestID != "" {
		valxRequestId, err := formats.Parse("uuid", hdrXRequestID)
		if err != nil {
			return errors.InvalidType("X-Request-Id", "header", "strfmt.UUID", hdrXRequestID)
		}
		o.XRequestID = valxRequestId.(strfmt.UUID)
	}

	return nil
}

Previously generated code

func (o *AddResourceCreated) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	// response header Location

	location, err := formats.Parse("uri", response.GetHeader("Location"))
	if err != nil {
		return errors.InvalidType("Location", "header", "strfmt.URI", response.GetHeader("Location"))
	}
	o.Location = *(location.(*strfmt.URI))

	// response header X-Request-Id

	xRequestId, err := formats.Parse("uuid", response.GetHeader("X-Request-Id"))
	if err != nil {
		return errors.InvalidType("X-Request-Id", "header", "strfmt.UUID", response.GetHeader("X-Request-Id"))
	}
	o.XRequestID = *(xRequestId.(*strfmt.UUID))

	return nil
}

Swagger specification

    post:
      x-visibility: private
      tags: [servicegroup]
      operationId: addResource
      summary: Add Resource
      description: Add Resource
      consumes:
        - application/vnd.cia.v1+json
      parameters:
        - name: resource
          description: Body of Resource
          in: body
          required: true
          schema:
            $ref: "#/definitions/resource"
      responses:
          201:
            description: Resource Added
            headers:
              X-Request-Id:
                description: Unique identifier associated with request
                type: string
                format: uuid
              Location:
                description: URL to the newly added Resource
                type: string
                format: uri
          400:
            $ref: "#/responses/400"
          401:
            $ref: "#/responses/401"
          402:
            $ref: "#/responses/402"
          403:
            $ref: "#/responses/403"
          404:
            $ref: "#/responses/404"
          409:
            $ref: "#/responses/409"
          default:
            $ref: "#/responses/500"

Environment

swagger version: 0.26.1
go version: 1.15
OS: linux_amd64

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions