Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Golang][go-gin-server]can not use the param of the path #1122

Closed
kemokemo opened this issue Sep 26, 2018 · 5 comments
Closed

[Golang][go-gin-server]can not use the param of the path #1122

kemokemo opened this issue Sep 26, 2018 · 5 comments

Comments

@kemokemo
Copy link
Contributor

kemokemo commented Sep 26, 2018

Description

The path with param does not be generated properly. ex) /pet/{petId}

openapi-generator version

3.3.0-SNAPSHOT
(master)

OpenAPI declaration file content or url

extracted_petstore.yaml

openapi: 3.0.0
servers:
  - url: 'http://petstore.swagger.io/v2'
info:
  description: >-
    This is a sample server Petstore server. For this sample, you can use the api key
    `special-key` to test the authorization filters.
  version: 1.0.0
  title: OpenAPI Petstore
  license:
    name: Apache-2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: pet
    description: Everything about your Pets
paths:
  '/pet/{petId}':
    get:
      tags:
        - pet
      summary: Find pet by ID
      description: Returns a single pet
      operationId: getPetById
      parameters:
        - name: petId
          in: path
          description: ID of pet to return
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: successful operation
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid ID supplied
        '404':
          description: Pet not found
      security:
        - api_key: []
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
components:
  requestBodies:
    Pet:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Pet'
        application/xml:
          schema:
            $ref: '#/components/schemas/Pet'
      description: Pet object that needs to be added to the store
      required: true
  schemas:
    Pet:
      title: a Pet
      description: A pet for sale in the pet store
      type: object
      required:
        - name
        - photoUrls
      properties:
        id:
          type: integer
          format: int64
        category:
          $ref: '#/components/schemas/Category'
        name:
          type: string
          example: doggie
        photoUrls:
          type: array
          xml:
            name: photoUrl
            wrapped: true
          items:
            type: string
        tags:
          type: array
          xml:
            name: tag
            wrapped: true
          items:
            $ref: '#/components/schemas/Tag'
        status:
          type: string
          description: pet status in the store
          enum:
            - available
            - pending
            - sold
      xml:
        name: Pet
Command line used for generation
generate -i extracted_petstore.yaml -g go-gin-server -o go-gin-server
Steps to reproduce
Related issues/PRs

Issue #1047
PR #1048

Suggest a fix/enhancement

routers.go generated by the current master.

var routes = Routes{
	{
		"Index",
		"GET",
		"/v2/",
		Index,
	},

	{
		"GetPetById",
		strings.ToUpper("Get"),
		"/v2/pet/{petId}",
		GetPetById,
	},
}

This code should be below:

var routes = Routes{
	{
		"Index",
		"GET",
		"/v2/",
		Index,
	},

	{
		"GetPetById",
		strings.ToUpper("Get"),
		"/v2/pet/:petId",
		GetPetById,
	},
}
@kemokemo
Copy link
Contributor Author

kemokemo commented Sep 26, 2018

I am sorry that the operation check was insufficient. I'll try fix this.
The {{{path}}} parameter passed to the routers.mustache has to be converted.

I will try to refer to ErlangServerCodegen.java which is doing similar processing.

@kemokemo
Copy link
Contributor Author

Hmm..
The gin has the specification gin-gonic/gin#388. I did not know until today.
I think that it's their design decision.

By modifying GoGinServerCodegen.java, I can generate the intended code on my local branch.
However, stub code generated from the standard petstore.yaml due to the specification of gin above does not work with the following error.

Server started
[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /v2/                      --> github.com/kemokemo/go-gin-server/go.Index (3 handlers)
[GIN-debug] POST   /v2/pet                   --> github.com/kemokemo/go-gin-server/go.AddPet (3 handlers)
[GIN-debug] DELETE /v2/pet/:petId            --> github.com/kemokemo/go-gin-server/go.DeletePet (3 handlers)
[GIN-debug] GET    /v2/pet/findByStatus      --> github.com/kemokemo/go-gin-server/go.FindPetsByStatus (3 handlers)
[GIN-debug] GET    /v2/pet/findByTags        --> github.com/kemokemo/go-gin-server/go.FindPetsByTags (3 handlers)
[GIN-debug] GET    /v2/pet/:petId            --> github.com/kemokemo/go-gin-server/go.GetPetById (3 handlers)
panic: wildcard route ':petId' conflicts with existing children in path '/v2/pet/:petId'

The endpoint /v2/pet/findByTags and /v2/pet/:petId are conflict with gin...

Is this disqualified as a generator?

@wing328
Copy link
Member

wing328 commented Sep 26, 2018

  "/v2/pet/:petId",

The other generators (e.g. finch) has a similar patch to correct the path parameter. Let me know if you need the code snippet to fix the same in the Go Gin generator.

@wing328
Copy link
Member

wing328 commented Sep 26, 2018

The endpoint /v2/pet/findByTags and /v2/pet/:petId are conflict with gin...

We can document this in the auto-generated README as a known issue with a reference to gin-gonic/gin#388

Users can manually fix it by updating the path to workaround the issue for the time being.

@kemokemo
Copy link
Contributor Author

@wing328 Thank you so much for your advise!
I am sorry that I caught a cold and the response was delayed.

I will post a PR with the following changes.

  • fix the 'the param of the path' issue
  • add a known issue and workaround to the README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants