-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add json-encoding output option for controlling JSON encoder settings #2371
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
base: main
Are you sure you want to change the base?
Changes from all commits
d731d57
de31d2d
eddb8d5
492c19e
7b3e403
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,19 +54,19 @@ func (a {{.TypeName}}) MarshalJSON() ([]byte, error) { | |
| object := make(map[string]json.RawMessage) | ||
| {{range .Schema.Properties}} | ||
| {{if .RequiresNilCheck}}if a.{{.GoFieldName}} != nil { {{end}} | ||
| object["{{.JsonFieldName}}"], err = json.Marshal(a.{{.GoFieldName}}) | ||
| object["{{.JsonFieldName}}"], err = {{jsonMarshalFieldExpr (printf "a.%s" .GoFieldName)}} | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error marshaling '{{.JsonFieldName}}': %w", err) | ||
| } | ||
| {{if .RequiresNilCheck}} }{{end}} | ||
| {{end}} | ||
| for fieldName, field := range a.AdditionalProperties { | ||
| object[fieldName], err = json.Marshal(field) | ||
| object[fieldName], err = {{jsonMarshalFieldExpr "field"}} | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error marshaling '%s': %w", fieldName, err) | ||
| } | ||
| } | ||
| return json.Marshal(object) | ||
| return {{jsonMarshalExpr "object"}} | ||
| } | ||
|
Comment on lines
54
to
70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When The same pattern applies in |
||
| {{end}} | ||
| {{end}} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestJSONEncodingOptionsonly enablesGenerateOptions{Models: true}, so the changes toclient.tmpl(jsonMarshalExpr "body"on the request body path) andstrict-interface.tmpl(jsonNewEncoder "buf"on the response encoding path) are never exercised with custom encoding settings. If an IIFE-based expression were syntactically invalid in either context, this test would not catch it. Adding sub-cases withGenerate.Client: trueandGenerate.StrictServer: true(paired with a minimal spec that has a JSON request body / JSON response) would close the gap.