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

🐛 array form filed name should not contains bracket which led to invalid fieldname in ts codegen #1706

Merged
merged 1 commit into from Jan 16, 2024

Conversation

Jinof
Copy link
Contributor

@Jinof Jinof commented Nov 20, 2023

Describe the PR
Change FormName. Trim bracket in filedname

Relation issue
e.g. https://github.com/swaggo/swag/pull/118/files

Additional context
In gin binding, an array parameter goes like

type Teacher struct {
  Books []string `form:"books[]"`
}

In swaggo/swag less than v1.8.10, it's field name should comes from ps.Filed.Names[0] which is "Names", which goes to toLowerCamelCase and generate "names".
image

func (ps *tagBaseFieldParser) FieldName() (string, error) {
	var name string
	if ps.field.Tag != nil {
		// json:"tag,hoge"
		name = strings.TrimSpace(strings.Split(ps.tag.Get(jsonTag), ",")[0])

		if name != "" {
			return name, nil
		}
	}

	if ps.field.Names == nil {
		return "", nil
	}

	switch ps.p.PropNamingStrategy {
	case SnakeCase:
		return toSnakeCase(ps.field.Names[0].Name), nil
	case PascalCase:
		return ps.field.Names[0].Name, nil
	default:
		return toLowerCamelCase(ps.field.Names[0].Name), nil
	}
}

Since v1.8.11, FormName was introduced to parse form tag, which generates books[] tags, which lead to bad codegen in ts.
Ts code changes, which is not generated by tools, write by hand.
From:

const Teacher = {
  books: []
}

To:

const Teacher = {
  "books[]": []
}

Teacher."books[]" is not working in ts, which only can be use like Teacher["books[]"].

@sdghchj
Copy link
Member

sdghchj commented Dec 18, 2023

Hello, rebase your branch on the latest master, please.

@Jinof Jinof force-pushed the master branch 2 times, most recently from dd220d1 to b9aba73 Compare December 18, 2023 12:49
@Jinof
Copy link
Contributor Author

Jinof commented Dec 18, 2023

Hello, rebase your branch on the latest master, please.

Already rebase to latest master.

@sdghchj
Copy link
Member

sdghchj commented Jan 9, 2024

Rebase again, please.

@Jinof
Copy link
Contributor Author

Jinof commented Jan 15, 2024

Rebase again, please.

done

@sdghchj sdghchj merged commit 76695ca into swaggo:master Jan 16, 2024
10 checks passed
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.

None yet

2 participants