Skip to content

Commit 859e08a

Browse files
authored
fix(go-sdk): generated unmarshalJSON in oneOf can not be compiled (#204)
relates to STACKITSDK-269
1 parent 513b733 commit 859e08a

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

blacklist.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ featuretoggle
44
workflows
55
openstack
66
notebooks
7-
observability
87
cost
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isArray}}[{{#items}}{{#isString}}"example"{{/isString}}{{/items}}]{{/isArray}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isContainer}}{{#isMap}}{"map":{{#items}}{{> custom/helper/array_json_example_value}}{{^isArray}}{{#isString}}"example"{{/isString}}{{/isArray}}{{/items}}}{{/isMap}}{{^isMap}}{{> custom/helper/array_json_example_value}}{{/isMap}}{{/isContainer}}

templates/go/custom/model_oneof_test.mustache

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,23 @@ func Test{{{classname}}}_UnmarshalJSON(t *testing.T) {
2727
wantErr: false,
2828
},
2929
{{/allowableValues.values}}
30-
{{^allowableValues.values}}{{^isModel}}
30+
{{^allowableValues.values}}{{^isModel}}{{^isContainer}}
3131
{
32-
name: "success - {{dataType}} {{example}}",
32+
name: "success - {{dataType}} {{{example}}}",
3333
args: args{
34-
src: []byte(`"{{example}}"`),
34+
src: []byte(`"{{{example}}}"`),
3535
},
3636
wantErr: false,
3737
},
38-
{{/isModel}}{{/allowableValues.values}}
38+
{{/isContainer}}{{#isContainer}}
39+
{
40+
name: "success - {{dataType}}",
41+
args: args{
42+
src: []byte(`{{>custom/helper/container_json_example_value}}`),
43+
},
44+
wantErr: false,
45+
},
46+
{{/isContainer}}{{/isModel}}{{/allowableValues.values}}
3947
{{#-last}}
4048
}
4149
for _, tt := range tests {

templates/go/model_oneof.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
6565
dst{{classname}}{{-index}} := &{{classname}}{}
6666
err = json.Unmarshal(data, &dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
6767
if err == nil {
68-
json{{{.}}}, _ := json.Marshal(&dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
68+
json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, _ := json.Marshal(&dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
6969
{{#pattern}}
7070
regex := `{{.}}`
7171
regex = regexp.MustCompile("^\\/|\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash
7272
regex = regexp.MustCompile("\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots
7373
rawString := regexp.MustCompile(`^"|"$`).ReplaceAllString(*dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{dataType}}{{/lambda.type-to-name}}, "$1") // Remove quotes
7474
isMatched, _ := regexp.MatchString(regex, rawString)
7575
{{/pattern}}
76-
if string(json{{{.}}}) != "{}" {{#pattern}}&& isMatched {{/pattern}} { // empty struct
76+
if string(json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) != "{}" {{#pattern}}&& isMatched {{/pattern}} { // empty struct
7777
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = dst{{classname}}{{-index}}.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
7878
match++
7979
}

0 commit comments

Comments
 (0)