fix(client): correctly marshal text/plain requests#1975
Conversation
As noted in #1914, there are cases where trying to interact with a `text/plain` endpoint that requires input, for instance when receiving a UUID, may not render correctly. We should first check if the type is a `Stringer`, aka has a `String()` method, and use that - otherwise use `fmt.Sprintf("%v", ...)` to generate a string type. Via [0], we can make sure that we wrap the generated type in an empty `interface`, so we can perform the type assertion. This also adds a test case to validate the functionality for: - a UUID, which has a `String()` method - a `float32`, which is a primitive datatype that needs to use `fmt.Sprintf` Co-authored-by: claude-sonnet:3.7-thinking <github-copilot@jamietanna.co.uk> Closes #1914. [0]: https://www.jvt.me/posts/2025/05/10/go-type-assertion-concrete/
|
@jamietanna any chance of this getting merged soon? |
For text/plain request bodies, the generated client code now:
1. Checks if the body type implements fmt.Stringer and uses String()
2. Falls back to fmt.Sprintf("%v", body) for primitive types
3. Returns an error for complex types (objects, arrays, composed
schemas), directing the user to implement a String() method
Adds Schema.IsPrimitive() to distinguish primitive OpenAPI types
(string, integer, number, boolean) from complex ones at codegen time.
Closes #1914.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
See this comment on a related PR: I've updated this PR, implementing that comment. I think this solves everyone's use case, and it errors out on impossible, undefined behavior. |
Co-authored-by: Gaiaz Iusipov <g.iusipov@gmail.com>
Greptile SummaryThis PR fixes incorrect Key changes:
One architectural concern: Confidence Score: 3/5
|
| Filename | Overview |
|---|---|
| pkg/codegen/schema.go | Adds IsPrimitive() helper that returns true for string/integer/number/boolean OpenAPI schema types. Implementation is clean and guards against nil OAPISchema. |
| pkg/codegen/templates/client.tmpl | Replaces the naive string(body) conversion with a Stringer-first, then primitive-fallback strategy. Key concern: IsPrimitive() gates the fallback on the OpenAPI schema type, which diverges from the actual Go type when x-go-type extensions are in use, potentially silencing serialization errors. |
| internal/test/issues/issue-1914/client_test.go | Adds two targeted tests covering UUID (Stringer path) and float32 (fmt.Sprintf path). Tests are clear and use appropriate assertions; float assertion is reliable on modern Go but lacks an explanatory comment. |
Last reviewed commit: 6ebcea7
As noted in #1914, there are cases where trying to interact with a
text/plainendpoint that requires input, for instance when receiving aUUID, may not render correctly.
We should first check if the type is a
Stringer, aka has aString()method, and use that - otherwise use
fmt.Sprintf("%v", ...)togenerate a string type.
Via 0, we can make sure that we wrap the generated type in an empty
interface, so we can perform the type assertion.This also adds a test case to validate the functionality for:
String()methodfloat32, which is a primitive datatype that needs to usefmt.SprintfCo-authored-by: claude-sonnet:3.7-thinking
Closes #1914.
Supersedes #1922.