Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Go] useOneOfDiscriminatorLookup generates invalid variable names for oneOf with no discriminator #18922

Open
5 of 6 tasks
sazapp-spectra opened this issue Jun 14, 2024 · 0 comments

Comments

@sazapp-spectra
Copy link

Bug Report Checklist

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

I'm trying to define a property in a schema that can be one of two types - a single date-time string or an array of strings. I'm using oneOf without a discriminator to do this. Also, in this same (rather large) schema, we have a few other instances of oneOf that do use discriminators, so we're setting useOneOfDiscriminatorLookup to true. The generated code looks something like this:

func (dst *BarProp) UnmarshalJSON(data []byte) error {
	var err error
	match := 0
	// try to unmarshal data into ArrayOfString
	err = json.Unmarshal(data, &dst.ArrayOfString)
	if err == nil {
		json[]string, _ := json.Marshal(dst.ArrayOfString)
		if string(json[]string) == "{}" { // empty struct
			dst.ArrayOfString = nil
		} else {
			match++
		}
	} else {
		dst.ArrayOfString = nil
	}

Note that json[]string isn't a valid variable name.

When I try it with useOneOfDiscriminatorLookup set to false, json[]string is named jsonArrayOfString, which compiles fine - this is what I'd expect the fix to do. In general, I'd expect the value of useOneOfDiscriminatorLookup to be irrelevant to code generated from a oneOf with no discriminator (more or less like what the linked issue seems to suggest).

openapi-generator version

7.5.0-SNAPSHOT

OpenAPI declaration file content or url

oneOf-array-of-strings.yaml

Generation Details
generate -g go -p useOneOfDiscriminatorLookup=true
Steps to reproduce
Related issues/PRs

[BUG][Go] UnmarshalJson has unused variable err if spec has oneOf without discriminator

Suggest a fix

I'm currently testing out just calling lambda.type-to-name in modules/openapi-generator/src/main/resources/go/model_oneof.mustache; specifically replacing the json{{.}} on line 61 with json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant