Skip to content

Issue with generated client when colon appears in API path #312

@bvwells

Description

@bvwells

There seems to be a bug is the way generated clients construct request paths from the server address and the operation path. The issue is shown when a colon appears in the API path, e.g. http://petstore.swagger.io/api/pets:validate.

The following spec illustrates the issue:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
  termsOfService: http://swagger.io/terms/
  contact:
    name: Swagger API Team
    email: apiteam@swagger.io
    url: http://swagger.io
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: http://petstore.swagger.io/api
paths:
  /pets:validate:
    post:
      summary: Validate pets
      description: Validate pets
      operationId: validatePets
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PetNames'
      responses:
        '200':
          description: valid pets
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'

        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    Pet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
          description: The name of the pet.

    PetNames:
      type: object
      required:
        - names
      properties:
        names:
          type: array
          description: The names of the pets.
          items:
            type: string

    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
          description: Error code
        message:
          type: string
          description: Error message

The generated client for this spec contains the following code:

func NewValidatePetsRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) {
	var err error

	queryUrl, err := url.Parse(server)
	if err != nil {
		return nil, err
	}

	basePath := fmt.Sprintf("/pets:validate")
	if basePath[0] == '/' {
		basePath = basePath[1:]
	}

	queryUrl, err = queryUrl.Parse(basePath)
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest("POST", queryUrl.String(), body)
	if err != nil {
		return nil, err
	}
...

The way in which the URL is constructed using queryUrl.Parse causes the request URL to be incorrect when the basePath parameter contains a colon. Using the generated client results in the following error

panic: Post "pets:validate": unsupported protocol scheme "pets"

This example shows the issue:

package main

import (
	"fmt"
	"net/url"
)

func main() {
	server := "http://petstore.swagger.io/api"
	queryUrl, err := url.Parse(server)
	if err != nil {
		panic(err)
	}

	basePath := fmt.Sprintf("/pets:validate")
	if basePath[0] == '/' {
		basePath = basePath[1:]
	}

	queryUrl, err = queryUrl.Parse(basePath)
	if err != nil {
		panic(err)
	}

	fmt.Printf("host=%s\n", queryUrl.Host)
	fmt.Printf("scheme=%s\n", queryUrl.Scheme)
	fmt.Printf("path=%s\n", queryUrl.Path)
}

This generates the following output

host=
scheme=pets
path=

See https://play.golang.org/p/JBkhGVWIFzV

I've put this example in the repo here: https://github.com/bvwells/oapi-codegen-bug

Let me know if you require any further details on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions