Skip to content

fix: escape quoted media type directives#2217

Merged
mromaszewicz merged 6 commits intooapi-codegen:mainfrom
brahmlower:fix/1529
Feb 13, 2026
Merged

fix: escape quoted media type directives#2217
mromaszewicz merged 6 commits intooapi-codegen:mainfrom
brahmlower:fix/1529

Conversation

@brahmlower
Copy link
Copy Markdown
Contributor

Fixes #1529

Added tests

  • validating the new function for quoting the go string
  • new "issues" test in internal/test/issues.

I appreciate yall keeping this project going- happy to make any requested changes!

@brahmlower brahmlower requested a review from a team as a code owner February 13, 2026 00:46
@brahmlower
Copy link
Copy Markdown
Contributor Author

And of course, as soon as I open the PR I realize I didn't actually enable the strict-server generation 🤦‍♂️

writer := multipart.NewWriter(w)
{{end -}}
w.Header().Set("Content-Type", {{if eq .NameTag "Multipart"}}{{if eq .ContentType "multipart/form-data"}}writer.FormDataContentType(){{else}}mime.FormatMediaType("{{.ContentType}}", map[string]string{"boundary": writer.Boundary()}){{end}}{{else if .HasFixedContentType }}"{{.ContentType}}"{{else}}response.ContentType{{end}})
w.Header().Set("Content-Type", {{if eq .NameTag "Multipart"}}{{if eq .ContentType "multipart/form-data"}}writer.FormDataContentType(){{else}}mime.FormatMediaType("{{.ContentType}}", map[string]string{"boundary": writer.Boundary()}){{end}}{{else if .HasFixedContentType }}{{.ContentType | toGoString}}{{else}}response.ContentType{{end}})
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This line is not easy to read. This change was as minimal as I could make it.

At first, I ignored the use of {{.ContentType}} in the call to mime.FormatMediaType, but now that I'm reviewing this more, the same sort of bug can arise when

  • eq .NameTag "Multipart" is true, and
  • eq .ContentType "multipart/form-data" is false

It's not immediately clear to me what part of the oapi spec the NameTag field refers to. I'll figure it out and include a fix and test later this evening.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've looked into the use of mime.FormatMediaType("{{.ContentType}}", ....

Testing locally, I found that changing it to use the toGoString function will result in function name collisions when multiple content types are defined. I managed to work around that by changing these lines to:

case strings.HasPrefix(contentType, "multipart/"):
	tag = mediaTypeToCamelCase(contentType)

However that caused name changes for multiple generated functions, which I think counts as a breaking change.

It seems unlikely that someone would have several possible multipart responses using quoted directives, so I don't think it's worth changing this mime.FormatMediaType call in this PR. Better to make that change in a separate PR, so greater focus can be given to possible breaking changes, if that's even something yall want to entertain.

mromaszewicz
mromaszewicz previously approved these changes Feb 13, 2026
@mromaszewicz
Copy link
Copy Markdown
Member

Make sure that make test and make lint work at the top level.

@mromaszewicz mromaszewicz self-requested a review February 13, 2026 17:58
@mromaszewicz mromaszewicz dismissed their stale review February 13, 2026 17:59

CI is finding lots of issues, need to manually review again.

@brahmlower
Copy link
Copy Markdown
Contributor Author

I'm learning a lot about the projects CI setup this morning 😁

@brahmlower
Copy link
Copy Markdown
Contributor Author

Most of the failures seemed to be small things I missed with the tests. Like a Makefile being expected in the fiber tests directory because it had a go mod file, then correctly including the oapi-codegen tool definition in go mod, and then subsequently failing because matrix tests covered versions of go that don't support the tool directive.

Definitely several silly mistakes on my part, but mostly test related.

I had hoped that rebasing via the github ui would keep the commit history somewhat cleaner, but looks like it represented rebase the same as if I had force pushed from my local. So, sorry about that

@brahmlower
Copy link
Copy Markdown
Contributor Author

I've really stumbled my way through the go version matrix tests- sorry about the mess of commits

@mromaszewicz mromaszewicz merged commit 01d4fc0 into oapi-codegen:main Feb 13, 2026
26 checks passed
@jamietanna jamietanna added the bug Something isn't working label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI escaped strings are not escaped in Go

3 participants