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

Go Swagger Annotation Considered deadcode #1304

Closed
shanedetsch opened this issue Aug 12, 2020 · 10 comments
Closed

Go Swagger Annotation Considered deadcode #1304

shanedetsch opened this issue Aug 12, 2020 · 10 comments
Labels

Comments

@shanedetsch
Copy link

The golangci-lint deadcode lint incorrectly considers swagger annotation as dead code.

The golangci-lint version

# golangci-lint --version
golangci-lint has version 1.30.0 built from 45b90f6 on 2020-08-03T03:10:34Z

The golangcli-lint validation failure

  {
    "description": "deadcode: `appSwaggerParameterDefinition` is unused",
    "fingerprint": "A4AB72*********E99AEAA3D",
    "location": {
      "path": "app/server.go",
      "lines": {
        "begin": 110
      }
    }
  },

However this is not dead code as it is a type of swagger annotation.

// DEFINED HERE
// Swagger parameters for the post endpoint
// swagger:parameters SwaggerParameterDefinition
type appSwaggerParameterDefinition struct {
	// The POST body for creating an Item
	// in: body
	// required: true
	Body service.RequestItemForPost
	// The integration type
	// in: path
	// required: true
	// example: {genericType}
	itemType string `json: "itemType"`
}

// USED HERE
// Swagger Docs for the post sql server endpoint where SwaggerParameterDefinition is a link to the Swagger parameters for this endpoint
// swagger:route POST /{itemType}/endpoint SwaggerParameterDefinition
// Returns created Item
// Schemes: http
// Responses:
//  200: okResponse
//  500: errorResponse
// handlePostItem creates an Item
func (s *IntegrationService) handlePostItem(c echo.Context) error {

@shanedetsch shanedetsch added the bug Something isn't working label Aug 12, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented Aug 12, 2020

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

@shanedetsch
Copy link
Author

shanedetsch commented Aug 12, 2020

Work around

// nolint:deadcode
 type appSwaggerParameterDefinition struct {
        // The POST body for creating an Item

@SVilgelm
Copy link
Member

SVilgelm commented Aug 12, 2020

It's not mentioned anywhere, you need to use it somewhere in the code. Now the deadcode linter show the correct message, the appSwaggerParameterDefinition is unused in provided sample

I would suggest you to use it in the handler:

func (s *IntegrationService) handlePostItem(c echo.Context) error {
var doc appSwaggerParameterDefinition
c.Bind(&doc)
...
}

@SVilgelm SVilgelm added declined and removed bug Something isn't working labels Aug 12, 2020
@shanedetsch
Copy link
Author

@SVilgelm Your solution is clever, however I think Go Swagger ties the annotation in the handler and struct definition so instantiating the struct from the context in the handler isn't necessary.

@SVilgelm
Copy link
Member

nope, it's not doing any magic, appSwaggerParameterDefinition should be used in generated code then.

If you have a generated code and that code uses the appSwaggerParameterDefinition and the dead code ignores that usage, only then it's a bug

otherwise you have a definition, but go does not use it and it probably uses map[string]interface{} instead of the appSwaggerParameterDefinition struct

@shanedetsch
Copy link
Author

Well the code is not used in production however the Swagger annotation is used by the ( swagger ) cli which will use a code ( model scan ) to generate apidocs. Thus maybe the best solution is to just use ( nolint:deadcode ).

@SVilgelm
Copy link
Member

SVilgelm commented Aug 13, 2020

actually you just store the definitions and uses them in another project. In this case yes you are right, using the nolint:deadcode is a solution, but it's definitely not a bug of the deadcode linter

@shanedetsch
Copy link
Author

Yeah right probably not a ( deadcode ) bug I see now.

@SVilgelm
Copy link
Member

so. I'm closing the issue

@shanedetsch
Copy link
Author

Yeah close, hopefully other people who are confused by swaggers interaction with deadcode can find this thread useful and use ( nolint:deadcode )

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

No branches or pull requests

2 participants