OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[BUG][GO] Discriminator not rendering #16016

Open wandergeek opened 1 year ago

wandergeek commented 1 year ago

Bug Report Checklist

Description

I've added a discriminator to our openAPI spec, and I can't seem to get the generated client to unmarshal the response properly.

❯ go run main.go 
panic: unable to unmarshal data into slo.SloResponse: data matches more than one schema in oneOf(SloResponseIndicator)

goroutine 1 [running]:
main.main()
        /Users/nick/work/busted-slo-parsing/main.go:149 +0x148
exit status 2

I would expect some code in this file to check the type field and unmarshal it appropriately.

openapi-generator version
❯ docker run --rm  openapitools/openapi-generator-cli:latest  version
7.0.0-SNAPSHOT
OpenAPI declaration file content or url

https://github.com/wandergeek/busted-slo-parsing/blob/master/bundled.yaml#L1341-L1353

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate \
        -i /local/bundled.yaml \
        --git-repo-id terraform-provider-elasticstack \
        --git-user-id elastic \
        -p isGoSubmodule=true \
        -p packageName=slo \
        -p generateInterfaces=true \
        -g go \
        -o /local/generated/slo
Steps to reproduce
git clone git@github.com:wandergeek/busted-slo-parsing.git
cd busted-slo-parsing
make generate-slo-client
go run main.go

Have I just specified this incorrectly? Any help much appreciated!

wing328 commented 1 year ago

can you try enabling the option useOneOfDiscriminatorLookup (e.g. via --additional-properties in CLI) ?

wandergeek commented 1 year ago

Thanks, @wing328! We seem to be getting somewhere now! That said, it's introduced a new issue in another part of the client. The error and offending line is:

expected operand, found ']'

        json[]WeightedCompositeSourcesInner, _ := json.Marshal(dst.ArrayOfWeightedCompositeSourcesInner)

Change is below- this is the diff before and after using useOneOfDiscriminatorLookup:

--- a/generated/slo/model_composite_slo_response_sources.go
+++ b/generated/slo/model_composite_slo_response_sources.go
@@ -27,15 +27,16 @@ func ArrayOfWeightedCompositeSourcesInnerAsCompositeSloResponseSources(v *[]Weig
        }
 }

+
 // Unmarshal JSON data into one of the pointers in the struct
 func (dst *CompositeSloResponseSources) UnmarshalJSON(data []byte) error {
        var err error
        match := 0
        // try to unmarshal data into ArrayOfWeightedCompositeSourcesInner
-       err = newStrictDecoder(data).Decode(&dst.ArrayOfWeightedCompositeSourcesInner)
+       err = json.Unmarshal(data, &dst.ArrayOfWeightedCompositeSourcesInner)
        if err == nil {
-               jsonArrayOfWeightedCompositeSourcesInner, _ := json.Marshal(dst.ArrayOfWeightedCompositeSourcesInner)
-               if string(jsonArrayOfWeightedCompositeSourcesInner) == "{}" { // empty struct
+               json[]WeightedCompositeSourcesInner, _ := json.Marshal(dst.ArrayOfWeightedCompositeSourcesInner)
+               if string(json[]WeightedCompositeSourcesInner) == "{}" { // empty struct
                        dst.ArrayOfWeightedCompositeSourcesInner = nil
                } else {
                        match++
@@ -66,7 +67,7 @@ func (src CompositeSloResponseSources) MarshalJSON() ([]byte, error) {
 }

 // Get the actual instance
-func (obj *CompositeSloResponseSources) GetActualInstance() interface{} {
+func (obj *CompositeSloResponseSources) GetActualInstance() (interface{}) {
        if obj == nil {
                return nil
        }
@@ -113,3 +114,5 @@ func (v *NullableCompositeSloResponseSources) UnmarshalJSON(src []byte) error {
        v.isSet = true
        return json.Unmarshal(src, &v.value)
 }

Relevant parts of the schema are here: https://github.com/wandergeek/busted-slo-parsing/blob/master/bundled.yaml#L816-L848.

I suspect it's confused because there's only one element in oneOf? I think we expect this part of the API to have different types, but don't yet.

wandergeek commented 1 year ago

I removed the oneOf with the single element and got it all to work. Do you want me to create an issue for this or just leave it be?