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

Wrong formating for custom struct names #1150

Open
F0rzend opened this issue Feb 25, 2022 · 1 comment
Open

Wrong formating for custom struct names #1150

F0rzend opened this issue Feb 25, 2022 · 1 comment

Comments

@F0rzend
Copy link

F0rzend commented Feb 25, 2022

Wrong formating for custom struct names
When run swag fmt 2 spaces appear between @name and the name instead of one and the name is parsed as an empty string

To Reproduce
Steps to reproduce the behavior:

  1. Write a struct, for example
type User struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
} // @name User
  1. Run swag fmt -g filename.go
  2. Get @name User instead of @name User in your code
  3. Run swag init -g path/to/mani.go
  4. In docs/swagger.yaml you will see
definitions:
  "":
    description: User information
    properties:
      id:
        type: integer
      username:
        type: string
    type: object

Expected behavior
Correct type name, "User"

Screenshots
image

Swag version

$ swag -v
swag version v1.8.0

Go version

$ go version
go version go1.18beta1 linux/amd64

Desktop:

  • OS: OS: Ubuntu 21.10 x86_64
  • Browser: Firefox browser 97.0 (64-bit); Mozila Firefox for Ubuntu canonical - 1.0
  • Version: 21.10
@ubogdan
Copy link
Contributor

ubogdan commented Mar 3, 2022

This is an open-source project. Feel free to contribute with a fix and I will be more than happy to assist you with a Code Review.

MattKetmo added a commit to MattKetmo/swag that referenced this issue Jun 30, 2022
Fix issue swaggo#1150

To summarize of the issue, running `swag fmt` will set (at least) 2 spaces between the attribute tag and the rest of the comment.

For instance:

```
type Example struct {
	Foobar string
} // @name  Example
```

However the doc generation doesn't interpret this correctly for struct (it completly ignore it).

So there are 2 solutions:

- fix the indent (which is feel more natural)
- fix the doc generation to correctly parse 2 spaces

This commit is fixing the indent part.

Basically I just set the padding=1 on `tabwriter`.

The cons is that it will also impacts all other godocs.

For instance (taken from the readme):

```
// @title          Swagger Example API
// @Version        1.0
// @description    This is a sample server celler server.
// @termsofservice http://swagger.io/terms/
```
MattKetmo added a commit to MattKetmo/swag that referenced this issue Jun 30, 2022
To summarize the issue, running `swag fmt` will set (at least) 2 spaces between the attribute tag and the rest of the comment. But this is a problem for struct with single comment:

```diff
type Example struct {
	Foobar string
-} // @name Example
+} // @name  Example
```

Indeed the doc generation doesn't interpret this correctly for struct (it completely ignore it).

So there are 2 solutions:

- fix the indent (which is feel more natural)
- fix the doc generation to correctly parse 2 spaces

This commit is fixing the indent part.

Basically I just set the padding=1 on `tabwriter`.

The cons is that it will also impacts all other godocs.

For instance (taken from the readme):

```diff
-// @title           Swagger Example API
-// @Version         1.0
-// @description     This is a sample server celler server.
-// @termsofservice  http://swagger.io/terms/
+// @title          Swagger Example API
+// @Version        1.0
+// @description    This is a sample server celler server.
+// @termsofservice http://swagger.io/terms/
```
MattKetmo added a commit to MattKetmo/swag that referenced this issue Jun 30, 2022
To summarize the issue, running `swag fmt` will set (at least) 2 spaces between the attribute tag and the rest of the comment. But this is a problem for struct with single comment:

```diff
type Example struct {
	Foobar string
-} // @name Example
+} // @name  Example
```

Indeed the doc generation doesn't interpret this correctly for struct (it completely ignore it).

So there are 2 solutions:

- fix the indent (which is feel more natural)
- fix the doc generation to correctly parse 2 spaces

This commit is fixing the indent part.

Basically I just set the padding=1 on `tabwriter`.

The cons is that it will also impacts all other godocs.

For instance (taken from the readme):

```diff
-// @title           Swagger Example API
-// @Version         1.0
-// @description     This is a sample server celler server.
-// @termsofservice  http://swagger.io/terms/
+// @title          Swagger Example API
+// @Version        1.0
+// @description    This is a sample server celler server.
+// @termsofservice http://swagger.io/terms/
```
ubogdan pushed a commit that referenced this issue Jun 30, 2022
To summarize the issue, running `swag fmt` will set (at least) 2 spaces between the attribute tag and the rest of the comment. But this is a problem for struct with single comment:

```diff
type Example struct {
	Foobar string
-} // @name Example
+} // @name  Example
```

Indeed the doc generation doesn't interpret this correctly for struct (it completely ignore it).

So there are 2 solutions:

- fix the indent (which is feel more natural)
- fix the doc generation to correctly parse 2 spaces

This commit is fixing the indent part.

Basically I just set the padding=1 on `tabwriter`.

The cons is that it will also impacts all other godocs.

For instance (taken from the readme):

```diff
-// @title           Swagger Example API
-// @Version         1.0
-// @description     This is a sample server celler server.
-// @termsofservice  http://swagger.io/terms/
+// @title          Swagger Example API
+// @Version        1.0
+// @description    This is a sample server celler server.
+// @termsofservice http://swagger.io/terms/
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants