OpenAPITools / OpenAPITools/openapi-generator

[BUG] go client with associative array combined with known key(s)

Open
#15,674 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ X] Have you provided a full/minimal spec to reproduce the issue?
  • [X ] Have you validated the input using an OpenAPI validator (example)?
  • [ X] Have you tested with the latest master to confirm the issue still exists?
  • [ X] Have you searched for related issues/PRs?
  • [ X] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hi, I'm trying to model a 3th party provided API to allow it easy consumed from go-lang.
Returned data looks like

{
 "3567843": {...},
 "9027374": {...},
...
"fixedkey1": "value1",
"fixedkey2": "value2"
}

Where the numbers are dynamic in value and in count.
I think this might be OAS3.1 stuff to have it decoded fully/recursive.

For now i started to try approach as associative array with a few fixed keys known.
Snippet of definition i tried:

 "components" : {
    "schemas" : {
      "TestEndPointConfigOk" : {
        "additionalProperties" : {
          "type" : "string"
        },
        "type" : "object"
      },
      "TestEndPointConfigNOK" : {
        "properties" : {
          "autoRebalance" : {
            "type" : "boolean"
          }
        },
        "required": [
          "autoRebalance"
        ],
        "additionalProperties" : {
          "type" : "string"
        },
        "type" : "object"
      }

    }
  },

TestEndPointConfigNOK is generated as a fixed struct, but seems to ignore the "additionalProperties"
TestEndPointConfigOK is generated as map[string]interface{}, which is OK.

openapi-generator versions tested

6.6.0 (and checked 7.0.0-latest daily build)

OpenAPI declaration file content or url
Generation Details

Just generate client:
openapi-generator generate -i demo.json -o ./gengo-client -g go --additional-properties packageName=app --additional-properties hideGenerationTimestamp=true

Steps to reproduce

After generation , in client library, look at func defined for

func (r ApiApiTestConfigurationGetRequest) Execute() (*TestEndPointConfigNOK, *http.Response, error) {
        return r.ApiService.ApiTestConfigurationGetExecute(r)
}

and the static definition for struct TestEndPointConfigNOK{}:

// TestEndPointConfigNOK struct for TestEndPointConfigNOK
type TestEndPointConfigNOK struct {
        AutoRebalance bool `json:"autoRebalance"`
}
Related issues/PRs
Suggest a fix

I would expect/like to see some additional code to support dynamic keys combined with static pre-defined keys.
for example struct definition:
// TestEndPointConfigNOK struct for TestEndPointConfigNOK
type TestEndPointConfigNOK struct {
AutoRebalance bool json:"autoRebalance",
DynamicKeys map[string]interface{} ,

}

and some helper functions generated for Marshall/Unmarshall and getting key/values.

(or something completely different which works :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied openapi-generator command with the included specification and inspect the generated Go client's TestEndPointConfigNOK struct and ApiApiTestConfigurationGetRequest.Execute entry point. Trace how schemas with properties and additionalProperties are handled; done means generated clients preserve both the fixed autoRebalance field and dynamic keys without breaking the existing map-only case.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.