Skip to content

fix: parse form name "-" as skipped field - #2090

Merged
sdghchj merged 1 commit into
swaggo:v2from
isqua:empty-form-field
Nov 5, 2025
Merged

sdghchj merged 1 commit into
swaggo:v2from
isqua:empty-form-field

Conversation

@isqua

@isqua isqua commented Oct 29, 2025

Copy link
Copy Markdown

Describe the PR

Given following struct with form:"-" annotation:

type CatchedFishForm struct {
	Count uint  `form:"count"`
	Fish  *Fish `form:"-"`
}

The tool had panic:

2025/10/29 22:35:34 Generating web.CatchedFishForm
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x2b8 pc=0x1031d6524]

goroutine 1 [running]:
github.com/swaggo/swag/v2.(*Parser).parseStructFieldV3(0x1400021ca00, 0x140002a61e0, 0x140002b4780)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:1006 +0x3e4
github.com/swaggo/swag/v2.(*Parser).parseStructV3(0x1400021ca00, 0x140002a61e0, 0x1400007fef0)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:885 +0xa8
github.com/swaggo/swag/v2.(*Parser).parseTypeExprV3(0x1400021ca00, 0x1032e6b0c?, {0x103527c08?, 0x1400000d1d0?}, 0x38?)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:808 +0x3e4
github.com/swaggo/swag/v2.(*Parser).ParseDefinitionV3(0x1400021ca00, 0x14006176fc0)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:718 +0x3a4
github.com/swaggo/swag/v2.(*Parser).getTypeSchemaV3(0x1400021ca00, {0x140000361fb, 0x13}, 0x140002a6000, 0x0)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:669 +0xa80
github.com/swaggo/swag/v2.(*OperationV3).ParseParamComment(0x1400825d000, {0x140000361ea, 0x3d}, 0x140002a6000)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:342 +0x928
github.com/swaggo/swag/v2.(*OperationV3).ParseComment(0x1400825d000, {0x140000361e0?, 0x1?}, 0x140002a6000)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:86 +0x190
github.com/swaggo/swag/v2.(*Parser).ParseRouterAPIInfoV3(0x1400021ca00, 0x1400028c700)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:514 +0x184
github.com/swaggo/swag/v2.(*PackagesDefinitions).RangeFiles(0x140001ff770, 0x14007fbf510)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/packages.go:107 +0x2c8
github.com/swaggo/swag/v2.(*Parser).ParseAPIMultiSearchDir(0x1400021ca00, {0x140002412c0, 0x1, 0x0?}, {0x1032df71c, 0x7}, 0x64)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parser.go:499 +0x4a4
github.com/swaggo/swag/v2/gen.(*Gen).Build(0x140001ff470, 0x140001de180)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/gen/gen.go:233 +0x608
main.initAction(0x14000224240)
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/cmd/swag/main.go:249 +0x7ec
github.com/urfave/cli/v2.(*Command).Run(0x140001ee420, 0x14000224240, {0x1400021ad20, 0xa, 0xa})
	~/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/command.go:274 +0x5d0
github.com/urfave/cli/v2.(*Command).Run(0x140001ee840, 0x14000224100, {0x14000132000, 0xb, 0xb})
	~/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/command.go:267 +0x808
github.com/urfave/cli/v2.(*App).RunContext(0x14000226000, {0x103528ca8, 0x1039b48c0}, {0x14000132000, 0xb, 0xb})
	~/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/app.go:332 +0x4f8
github.com/urfave/cli/v2.(*App).Run(...)
	~/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/app.go:309
main.main()
	~/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/cmd/swag/main.go:341 +0x58c

Relation issue

#1639

Comment thread field_parserv3.go
Comment on lines +518 to +521
name := strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0])
if name != "-" {
return name
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we consider form:"-" as a field with no name, so we return empty string in the end.

Comment thread field_parserv3.go
Comment on lines +526 to 534
func (ps *tagBaseFieldParserV3) JsonName() string {
if ps.field.Tag != nil {
name := strings.TrimSpace(strings.Split(ps.tag.Get(jsonTag), ",")[0])
if name != "-" {
return name
}
}
return ""
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of consistency, I also extracted the same method for JSON field names.

Comment on lines +136 to +139
type CatchedFishForm struct {
Count uint `form:"count"`
Fish *Fish `form:"-"`
}

@isqua isqua Oct 29, 2025 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you apply this changes of testdata/v3/simple/api/api.go and testdata/v3/simple/web/handler.go to the branch v2 and try to generate docs

swag init --v3.1

It'll panic. In this branch it works.

@sdghchj
sdghchj merged commit 1cb456d into swaggo:v2 Nov 5, 2025
1 check passed
@isqua
isqua deleted the empty-form-field branch December 19, 2025 12:47
abrar71 added a commit to abrar71/swag that referenced this pull request Dec 24, 2025
fix: parse form name "-" as skipped field (swaggo#2090)
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

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址