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] UnmarshalJson has unused variable err if spec has oneOf without discriminator #7961

Open
4 of 6 tasks
code-lucidal58 opened this issue Nov 17, 2020 · 4 comments
Open
4 of 6 tasks

Comments

@code-lucidal58
Copy link
Contributor

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

In the openAPI spec, if a schema has oneOf but does not have a discriminator and the config useOneOfDiscriminatorLookup is set to True, the unmarshalJson function is incorrectly generated. Such a scenario can arise when the schema looks as follows:

Pet:
  description: a pet
  oneOf:
  - type: 'null'
  - $ref: "#/components/schemas/dog"
  - $ref: "#/components/schemas/cat"

Considering the fact that there are other schemas in this spec that may have oneOf without type: 'null', the configuration useOneOfDiscriminatorLookup: True is required. As a result, the unmarshal function for the above object is as follows:

func (dst *Pet) UnmarshalJSON(data []byte) error {
	var err error
	// this object is nullable so check if the payload is null or empty string
	if string(data) == "" || string(data) == "{}" {
		return nil
	}
	return nil
}

Building this generates an error because the variable err is declared but not used.

openapi-generator version

Master build with last commit id 023e6dd

Suggest a fix

Allow unmarshalling of data even if nodiscriminator is mentioned whenuseOneOfDiscriminatorLookup: True

@wing328
Copy link
Member

wing328 commented Dec 2, 2020

I've filed #8069 to fix the issue.

@code-lucidal58
Copy link
Contributor Author

As per the change, if useOneOfDiscriminatorLookup is set to true, but the discriminator is missing, then the UnmarshalJSON function will be empty. Is this the desired result?

Expected: In the absence of discriminator, UnmarshalJSON can have the same code as when useOneOfDiscriminatorLookup is false.

@wing328
Copy link
Member

wing328 commented Dec 2, 2020

Good point. I'll see what I can do.

@kandaaaaa
Copy link
Contributor

Suggest to have this PR #11178

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

Successfully merging a pull request may close this issue.

3 participants