Skip to content

Commit 51d7bf7

Browse files
committed
fix: if oneOf contains multiple strings and the data doesn't match the last regex, the String attribute will be overwritten to nil
1 parent a97b651 commit 51d7bf7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

templates/go/custom/model_oneof_test.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ func Test{{{classname}}}_UnmarshalJSON(t *testing.T) {
4444
if err := v.UnmarshalJSON(tt.args.src); (err != nil) != tt.wantErr {
4545
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
4646
}
47+
marshalJson, err := v.MarshalJSON()
48+
if err != nil {
49+
t.Fatalf("failed marshalling {{classname}}: %v", err)
50+
}
51+
if string(marshalJson) != string(tt.args.src) {
52+
t.Fatalf("wanted %s, get %s", tt.args.src, marshalJson)
53+
}
4754
})
4855
}
4956
}

templates/go/model_oneof.mustache

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,21 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
5959
{{#composedSchemas.oneOf}}
6060
{{#dataType}}
6161
// try to unmarshal data into {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
62-
err = json.Unmarshal(data, &dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
62+
dst{{classname}}{{-index}} := &{{classname}}{}
63+
err = json.Unmarshal(data, &dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
6364
if err == nil {
64-
json{{{.}}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
65+
json{{{.}}}, _ := json.Marshal(&dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
6566
{{#pattern}}
6667
regex := `{{.}}`
6768
regex = regexp.MustCompile("^\\/|\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash
6869
regex = regexp.MustCompile("\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots
69-
rawString := regexp.MustCompile(`^"|"$`).ReplaceAllString(*dst.String, "$1") // Remove quotes
70+
rawString := regexp.MustCompile(`^"|"$`).ReplaceAllString(*dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{dataType}}{{/lambda.type-to-name}}, "$1") // Remove quotes
7071
isMatched, _ := regexp.MatchString(regex, rawString)
7172
{{/pattern}}
72-
if string(json{{{.}}}) == "{}" {{#pattern}}|| !isMatched {{/pattern}} { // empty struct
73-
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
74-
} else {
73+
if string(json{{{.}}}) != "{}" {{#pattern}}&& isMatched {{/pattern}} { // empty struct
74+
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
7575
match++
7676
}
77-
} else {
78-
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
7977
}
8078

8179
{{/dataType}}

0 commit comments

Comments
 (0)