glideapps / glideapps/quicktype

C#: add an opt-in Newtonsoft-compatible nullable reference output mode

Open
#3,097 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

## Problem

When generating C# models for an API whose object properties are optional, the Newtonsoft renderer can emit properties such as:

```csharp
[JsonProperty("profile", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
public Profile Profile { get; set; }
```

This is awkward for consumers using nullable reference types: the property can be absent from JSON, so its default value after deserialization is `null`, but the generated C# declaration is non-nullable. With `--check-required`, an explicit JSON `null` can also be rejected by Newtonsoft.Json.

The generated `FromJson` helper also returns a non-nullable model and directly propagates deserialization exceptions.

## Reproduction

Schema:

```json
{
"title": "Request",
"type": "object",
"properties": {
"profile": {
"type": "object",
"properties": {
"id": { "type": "string" }
}
},
"displayName": { "type": "string" }
}
}
```

Command:

```sh
quicktype \
--src request.schema.json \
--src-lang schema \
--lang csharp \
--framework NewtonSoft \
--csharp-version 6 \
--features complete \
--check-required \
--out Request.cs
```

## Requested behavior

Please add an opt-in renderer option, without changing existing defaults, for consumers that want this compatibility behavior:

- enable nullable reference annotations for optional reference properties;
- avoid emitting `Required.DisallowNull` for properties that may be absent;
- make the top-level `FromJson` helper's nullable result explicit;
- preserve the current output when the option is not supplied.

A possible name is `--csharp-nullable-reference-types` or another name that fits the existing option model.

Contributor guide

Open the contributing guide

Research direction

Start by locating the Newtonsoft C# renderer and the existing C# renderer-option and CLI-option handling. Run the reproduction command to capture current output, then verify that the opt-in mode changes optional reference annotations, Required handling, and FromJson's nullable result while the default output remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.