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

Client wont compile with a route tagged "test" #1683

Closed
mmmguitar opened this issue Sep 6, 2018 · 1 comment · Fixed by #1804
Closed

Client wont compile with a route tagged "test" #1683

mmmguitar opened this issue Sep 6, 2018 · 1 comment · Fixed by #1804

Comments

@mmmguitar
Copy link

mmmguitar commented Sep 6, 2018

Problem statement

Have a swagger with a route tagged "test", it can generate the client but it wont compile. The basic client file generated contains a struct referencing a package test_swagger which does not exist.

After a little digging it seems the template generation uses formatting function called snakize which changes the name (protection against reserved words) but not all formatting functions do this or use is inconsistent.

See here, this is the line that fails to compile for "test":

https://github.com/go-swagger/go-swagger/blob/master/generator/templates/client/facade.gotmpl#L120

This seems to boil down to a MangleFileName func which goes to a LanguageOpts fileNameFunc and there is a comment /code that does some special reserved word protection where it will append "_swagger" on to it

So it seems this struct is having that package manipulated / matched the reserved word but other usages are not, neither is the import which is why the struct package is not found / failing to compile.

An example of a place using .Name directly.
https://github.com/go-swagger/go-swagger/blob/master/generator/templates/client/facade.gotmpl#L73

The other thing I noticed for the client operations it generates a folder in its name, which is "client/test" -- i.e. it doesn't attempt to do the _swagger. So either snakize and some of the formatting functions are being a little over zealous or the _swagger notion is missing from a few places including the folder creations. Thats when I entered a rabbit hole.

I also found this ticket:

#1157

Which also seems to have some inconsistency related to use of formatting functions.

The simple workaround is to tag the route as something a little more specific / not use a reserved word.

Swagger specification

  /my/route/:
    get:
      operationId: getSomeData
      summary: Gets some data
      tags: [test]

Steps to reproduce

Generate a client with a swagger file that contains a route tagged with "test", will generate but shouldnt compile.

Environment

swagger version: 0.16.0
go version: 1.10.3
OS: Linux Mint 18

@fredbi
Copy link
Contributor

fredbi commented Sep 6, 2018

Looks like removing the snakize here

{{ pascalize .Name }} *{{ snakize .Name }}.Client

is enough to fix things.

On the other hand, snakize is missing here:

FileName: "{{ .Name }}Client.go",

This problem seems limited to client generation: no issue when testing with server codegen.

Also please note that some name conflicts are possible, and probably unavoidable like: choosing a tag like: "strfmt", "httptransport", "runtime", "models", "api" ... will almost certainly generate name conflicts.

fredbi added a commit to fredbi/go-swagger that referenced this issue Sep 6, 2018
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi self-assigned this Sep 10, 2018
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
Makes generation functional and consistent regarding:
* dashes in names
* reserved suffixes, which are meaningful for go, like 'test'
* sub-directories specified as target names (e.g. 'api/subdir')
* packages for client, model and server

* fixes go-swagger#1683
* fixes go-swagger#1157
* fixes go-swagger#1624

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
Makes generation functional and consistent regarding:
* dashes in names
* reserved suffixes, which are meaningful for go, like 'test'
* sub-directories specified as target names (e.g. 'api/subdir')
* packages for client, model and server

* fixes go-swagger#1683
* fixes go-swagger#1157
* fixes go-swagger#1624

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 18, 2018
Makes generation functional and consistent regarding:
* dashes in names
* reserved suffixes, which are meaningful for go, like 'test'
* sub-directories specified as target names (e.g. 'api/subdir')
* packages for client, model and server

* fixes go-swagger#1683
* fixes go-swagger#1157
* fixes go-swagger#1624

* replaces go-swagger#1790

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 18, 2018
Makes generation functional and consistent regarding:
* dashes in names
* reserved suffixes, which are meaningful for go, like 'test'
* sub-directories specified as target names (e.g. 'api/subdir')
* packages for client, model and server

* fixes go-swagger#1683
* fixes go-swagger#1157
* fixes go-swagger#1624

* replaces go-swagger#1790

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants