swagger-api / swagger-api/swagger-codegen
Generated C# POCOs throw a NRE when a collection property is null
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
C# POCOs with collection properties throw a NullReferenceException when the collection property is null and the #Equals() method of the generated POCO is called.
public bool Equals(CommentDTO input)
{
if (input == null)
return false;
return
(
this.Editors == input.Editors ||
this.Editors != null &&
this.Editors.SequenceEqual(input.Editors)
);
}
So when I have two CommentDTO instances and call c1.Equals(c2), a NRE is thrown when c2 hasn't any editors set (since input.Editors is null).
Example test:
[Test]
public void Test() {
CommentDTO c1 = new CommentDTO { Editors = new List<EditorDTO> { new EditorDTO() } };
CommentDTO c2 = new CommentDTO();
Assert.IsFalse(c1.Equals(c2));
}
Swagger-codegen version
2.3.1
Swagger declaration file content or url
Not specific to my API, but there you go: https://gist.github.com/molehillrocker/cc9d0ea54f3cb720c1e7726185c5c483
Command line used for generation
$java -jar swagger-codegen-cli.jar generate -i <url> -l csharp -o .\
Suggest a fix/enhancement
Either guard the check inside the #Equals() method or initialize all collection properties with an empty list/collection.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the generated C# POCO Equals method shown in the issue and reproduce it with the provided CommentDTO test and Swagger declaration. Generate the C# client with the stated swagger-codegen command, then verify that comparing objects with a null collection no longer throws and the assertion passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100