Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenAPI Tools
openapi-generator
Commits
af6242d6
Unverified
Commit
af6242d6
authored
5 years ago
by
Slavek Kabrda
Browse files
Options
Download
Email Patches
Plain Diff
[go-experimental] Fix usage of discriminator for oneOf deserialization
parent
55d8e759
github/fork/bkabrda/fix-go-discriminator-usage
1 merge request
!6672
[go-experimental] Fix usage of discriminator for oneOf deserialization
Pipeline
#475
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
+1
-1
...rc/main/java/org/openapitools/codegen/DefaultCodegen.java
modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache
+6
-7
...r/src/main/resources/go-experimental/model_oneof.mustache
with
7 additions
and
8 deletions
+7
-8
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
+
1
-
1
View file @
af6242d6
...
...
@@ -2539,7 +2539,7 @@ public class DefaultCodegen implements CodegenConfig {
private
CodegenProperty
discriminatorFound
(
String
composedSchemaName
,
Schema
sc
,
String
discPropName
,
OpenAPI
openAPI
)
{
Schema
refSchema
=
ModelUtils
.
getReferencedSchema
(
openAPI
,
sc
);
if
(
refSchema
.
getProperties
()
!=
null
&&
refSchema
.
getProperties
().
get
(
discPropName
)
!=
null
)
{
Schema
discSchema
=
(
Schema
)
refSchema
.
getProperties
().
get
(
discPropName
);
Schema
discSchema
=
ModelUtils
.
getReferencedSchema
(
openAPI
,
(
Schema
)
refSchema
.
getProperties
().
get
(
discPropName
)
)
;
CodegenProperty
cp
=
new
CodegenProperty
();
if
(
ModelUtils
.
isStringSchema
(
discSchema
))
{
cp
.
isString
=
true
;
...
...
This diff is collapsed.
Click to expand it.
modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache
+
6
-
7
View file @
af6242d6
...
...
@@ -16,7 +16,6 @@ func {{{.}}}As{{classname}}(v *{{{.}}}) {{classname}} {
// Unmarshal JSON data into one of the pointers in the struct
func (dst *
{{
classname
}}
) UnmarshalJSON(data []byte) error {
var err error
match := 0
{{#
isNullable
}}
// this object is nullable so check if the payload is null or empty string
if string(data) == "" || string(data) == "{}" {
...
...
@@ -41,20 +40,19 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
// try to unmarshal JSON data into
{{{
modelName
}}}
err = json.Unmarshal(data,
&
dst.
{{{
modelName
}}}
)
if err == nil {
json
{{{
modelName
}}}
, _ := json.Marshal(dst.
{{{
modelName
}}}
)
if string(json
{{{
modelName
}}}
) == "{}" { // empty struct
dst.
{{{
modelName
}}}
= nil
} else {
return nil // data stored in dst.
{{{
modelName
}}}
, return on the first match
}
return nil // data stored in dst.
{{{
modelName
}}}
, return on the first match
} else {
dst.
{{{
modelName
}}}
= nil
return fmt.Errorf("Failed to unmarshal
{{
classname
}}
as
{{{
modelName
}}}
: %s", err.Error())
}
}
{{/
mappedModels
}}
{{/
discriminator
}}
return nil
{{/
useOneOfDiscriminatorLookup
}}
{{^
useOneOfDiscriminatorLookup
}}
match := 0
{{#
oneOf
}}
// try to unmarshal data into
{{{
.
}}}
err = json.Unmarshal(data,
&
dst.
{{{
.
}}}
)
...
...
@@ -82,6 +80,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
} else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf(
{{
classname
}}
)")
}
{{/
useOneOfDiscriminatorLookup
}}
}
// Marshal data from the first non-nil pointers in the struct to JSON
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets