@@ -54,21 +54,34 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
5454 { {/discriminator} }
5555 { {^discriminator} }
5656 match := 0
57- { {#oneOf} }
57+ { {! BEGIN - Workaround in case oneOf contains enums and strings. The enum value would match with both. } }
58+ { {! This workaround adds a regex check for the string (in case the api spec provides a regex) } }
59+ // Workaround until upstream issue is fixed:
60+ // https://github.com/OpenAPITools/openapi-generator/issues/21751
61+ // Tracking issue on our side: https://jira.schwarz/browse/STACKITSDK-226
62+ { {#composedSchemas.oneOf} }
63+ { {#dataType} }
5864 // try to unmarshal data into { {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} }
59- err = json.Unmarshal(data, &dst.{ {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} })
65+ dst{ {classname} }{ {-index} } := & { {classname} }{ }
66+ err = json.Unmarshal(data, &dst{ {classname} }{ {-index} }.{ {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} })
6067 if err == nil {
61- json{{{.} }}, _ := json.Marshal(dst.{ {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} })
62- if string(json{ {{.} }}) == "{ } " { // empty struct
63- dst.{{#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} } = nil
64- } else {
68+ json{{{.} }}, _ := json.Marshal(&dst{ {classname} }{ {-index} }.{ {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} })
69+ { {#pattern} }
70+ regex := `{ {.} }`
71+ regex = regexp.MustCompile("^\\/|\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash
72+ regex = regexp.MustCompile("\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots
73+ rawString := regexp.MustCompile(`^"|"$`).ReplaceAllString(*dst{ {classname} }{ {-index} }.{ {#lambda.type-to-name} }{ {dataType} }{ {/lambda.type-to-name} }, "$1") // Remove quotes
74+ isMatched, _ := regexp.MatchString(regex, rawString)
75+ { {/pattern} }
76+ if string(json{ {{.} }}) != "{ } " { {#pattern} }&& isMatched { {/pattern} } { // empty struct
77+ dst.{{#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} } = dst{ {classname} }{ {-index} }.{ {#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} }
6578 match++
6679 }
67- } else {
68- dst.{{#lambda.type-to-name} }{ {{.} }}{ {/lambda.type-to-name} } = nil
6980 }
7081
71- { {/oneOf} }
82+ { {/dataType} }
83+ { {/composedSchemas.oneOf} }
84+ { {! END - Workaround in case oneOf contains enums and strings. The enum value would match with both. } }
7285 if match > 1 { // more than 1 match
7386 // reset to nil
7487 {{#oneOf} }
0 commit comments