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

Fix Null Reference when Schema is null in a specific circumstance. #3203

Merged
merged 21 commits into from
Dec 17, 2020

Conversation

ksmithRenweb
Copy link
Contributor

The GetTypeName function handles a null Schema when it is passed in but the hint parameter is attempting to use the Schema assuming it is not null. This is where the NullReferenceException is generated. With a couple of extra lines of code, that case is handled.

The other option is to make the GetTypeName hint default with null and inside the method if Schema is not null but hint is null. Then do the code, "hint = !response.Value.Schema.HasTypeNameTitle ? "Response" : null"

Copy link
Collaborator

@jeremyVignelles jeremyVignelles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the behavior should be here, I don't have enough context, but the change seems to improve null safety here

src/NSwag.CodeGeneration/Models/OperationModelBase.cs Outdated Show resolved Hide resolved
@RicoSuter
Copy link
Owner

I'd like to merge but there are conflicts. Can you fix those?

@ksmithRenweb
Copy link
Contributor Author

ksmithRenweb commented Dec 16, 2020

I'd like to merge but there are conflicts. Can you fix those?

It looks like the conflicts are from equivalent code that you commited an hour before your comment. See below.
Do you prefer the more verbose style I have, or the concise style you commited?

<<<<<<< fixNullRef
                var schemaHasTypeNameTitle = response.Value.Schema?.HasTypeNameTitle;
                var hint = schemaHasTypeNameTitle != true ? "Response" : null;
                return _generator.GetTypeName(response.Value.Schema, isNullable, hint);
=======
                return _generator.GetTypeName(response.Value.Schema, isNullable, response.Value.Schema?.HasTypeNameTitle != true ? "Response" : null);
>>>>>>> master

@RicoSuter
Copy link
Owner

I think the more verbose one is better.

@ksmithRenweb
Copy link
Contributor Author

@RicoSuter Merge conflict fixed.

@RicoSuter RicoSuter merged commit de42949 into RicoSuter:master Dec 17, 2020
@RicoSuter
Copy link
Owner

Thx.

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

Successfully merging this pull request may close these issues.

None yet

3 participants