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][golang][7.3.0] Generated model accessor functions have incorrect boolean logic for array types #17835

Closed
jduplaid opened this issue Feb 9, 2024 · 2 comments · Fixed by #17870

Comments

@jduplaid
Copy link

jduplaid commented Feb 9, 2024

Given a schema with a field that has type array like this:

"Foo": {
   "type": "object",
   "properties": {
        "data": {
           "type": "array",
           "items": {
              "$ref": "#/components/schemas/Bar"
           },
           "nullable": true
        },
        ...
    } 
}

our generated models have functions like:

// HasData returns a boolean if a field has been set.
func (o *Foo) HasData() bool {
	if o != nil && IsNil(o.Data) {
		return true
	}

	return false
}

shouldn't the boolean to check if the Foo model has data be o != nil && !IsNil(o.Data), rather than o != nil && IsNil(o.Data)?

another accessor function also gets generated:

func (o *Foo) GetDataOk() ([]Bar, bool) {
	if o == nil || IsNil(o.Data) {
		return nil, false
	}
	return o.Data, true
}

GetDataOk seems correct, and applying DeMorgan's law, that indicates HasData should use o != nil && !IsNil(o.Data)

We are using v7.3 openapi-generator, but we believe the issue is present in 7.2 as well

@jduplaid jduplaid changed the title [BUG][golang][7.3.0] Generated models have incorrect accessor function logic for array types [BUG][golang][7.3.0] Generated models accessor functions have incorrect boolean logic for array types Feb 9, 2024
@jduplaid jduplaid changed the title [BUG][golang][7.3.0] Generated models accessor functions have incorrect boolean logic for array types [BUG][golang][7.3.0] Generated model accessor functions have incorrect boolean logic for array types Feb 9, 2024
sagarkhot3 added a commit to sagarkhot3/openapi-generator that referenced this issue Feb 10, 2024
@wing328
Copy link
Member

wing328 commented Feb 12, 2024

shouldn't the boolean to check if the Foo model has data be o != nil && !IsNil(o.Data), rather than o != nil && IsNil(o.Data)?

I think so

can you please file a PR when you've time?

@jduplaid
Copy link
Author

@wing328 looks like @sagarkhot3 already created a fix here: #17840, if you can help take a look at that - thanks!

@wing328 wing328 mentioned this issue Feb 15, 2024
5 tasks
wing328 added a commit that referenced this issue Feb 15, 2024
* #17835 changed to return !IsNil

* update samples

---------

Co-authored-by: sagar khot <sagarkhot74@gmail.com>
kota65535 pushed a commit to kota65535/openapi-generator that referenced this issue Feb 23, 2024
* OpenAPITools#17835 changed to return !IsNil

* update samples

---------

Co-authored-by: sagar khot <sagarkhot74@gmail.com>
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.

2 participants