swagger-api / swagger-api/swagger-codegen-generators
[C#] Csharp Client SDK fails to deserialize result with required fields in schema
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
API spec declares schema object with required fields which is result of an operation.
Example:
paths:
/api/example-shemas:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
responses:
'202':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
---------------------------------
components:
schemas:
ExampleSchema:
required:
- reference_id
type: object
properties:
reference_id:
type: string
description:
CSharp generated constructor for the ExampleSchema model type is as follows:
public ExampleSchema(string referenceId = default(string))
{
// to ensure "referenceId" is required (not null)
if (referenceId == null) {
throw new InvalidDataException("referenceId is a required property for ExampleSchema and cannot be null");
} else {
this.ReferenceId = referenceId;
}
When CreateExampleSchema operation is called the ApiClient calls the default constructor of the ExampleSchema type when Deserialize the response from the server, the code is
JsonConvert.DeserializeObject(response.Content, type, serializerSettings)
Since the ExampleSchema constructor has one parameter with default value for C# this is the default constructor (the one without parameters). When it is called InvalidDataException is thrown because the default value of referenceId parameter is default(string) which is null.
Suggestion for a fix is to not generate defaults for required fields and generate default constructor that doesn't throw InvalidDataException.
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# model constructor and the ApiClient call to JsonConvert.DeserializeObject(response.Content, type, serializerSettings). Reproduce the required-field response case and determine how deserialization should avoid the constructor exception while preserving required-field behavior. Done means CreateExampleSchema can deserialize a response containing required fields without throwing InvalidDataException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100