Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ export class CSharpRenderer extends ConvenienceRenderer {
}

const csType = this.csType(t, follow, withIssues);
if (isValueType(t) || this._csOptions.version >= 8) {
if (
isValueType(t) ||
this._csOptions.version >= 8 ||
this._csOptions.nullableReferenceTypes
) {
return [csType, "?"];
} else {
return csType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
}

protected emitDefaultFollowingComments(): void {
if (!this._needHelpers || this._options.version < 8) return;
if (
!this._needHelpers ||
(this._options.version < 8 && !this._options.nullableReferenceTypes)
) {
return;
}

this.emitLine("#pragma warning restore CS8618");
this.emitLine("#pragma warning restore CS8601");
Expand Down Expand Up @@ -247,7 +252,10 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
);
});

if (this._options.version >= 8) {
if (
this._options.version >= 8 ||
this._options.nullableReferenceTypes
) {
this.emitLine("#nullable enable");
this.emitLine("#pragma warning disable CS8618");
this.emitLine("#pragma warning disable CS8601");
Expand Down Expand Up @@ -305,7 +313,10 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
? [", NullValueHandling = ", nullValueHandlingClass, ".Ignore"]
: [];
let required: Sourcelike;
if (!this._options.checkRequired || (isOptional && isNullable)) {
if (
!this._options.checkRequired ||
(isOptional && (isNullable || this._options.nullableReferenceTypes))
) {
required = [nullValueHandling];
} else if (isOptional && !isNullable) {
required = [
Expand Down Expand Up @@ -374,6 +385,9 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
}

const csType = this.topLevelResultType(t);
const fromJsonType = this._options.nullableReferenceTypes
? this.nullableCSType(t)
: csType;
this.emitType(
undefined,
AccessModifier.Public,
Expand All @@ -383,7 +397,7 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
() => {
// FIXME: Make FromJson a Named
this.emitExpressionMember(
["public static ", csType, " FromJson(string json)"],
["public static ", fromJsonType, " FromJson(string json)"],
[
"JsonConvert.DeserializeObject<",
csType,
Expand Down
6 changes: 6 additions & 0 deletions packages/quicktype-core/src/language/CSharp/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export const cSharpOptions = {
"8",
"secondary",
),
nullableReferenceTypes: new BooleanOption(
"nullable-reference-types",
"Use nullable reference types for optional properties (C# 8+)",
false,
"secondary",
),
virtual: new BooleanOption("virtual", "Generate virtual properties", false),
useRecords: new BooleanOption(
"use-records",
Expand Down
1 change: 1 addition & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export const CSharpLanguage: Language = {
// language-version code paths covered.
{ "csharp-version": "5" },
{ "csharp-version": "6" },
{ "csharp-version": "6", "nullable-reference-types": true },
{ density: "dense" },
{ "number-type": "decimal" },
{ "any-type": "dynamic" },
Expand Down
71 changes: 71 additions & 0 deletions test/unit/csharp-nullable-reference-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, test } from "vitest";

import {
InputData,
JSONSchemaInput,
type RendererOptions,
quicktype,
} from "../../packages/quicktype-core/src/index.js";

async function renderCSharp(rendererOptions: RendererOptions): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "Request",
schema: JSON.stringify({
type: "object",
properties: {
profile: {
type: "object",
properties: { id: { type: "string" } },
},
displayName: { type: "string" },
},
}),
});

const inputData = new InputData();
inputData.addInput(schemaInput);
const result = await quicktype({
inputData,
lang: "csharp",
rendererOptions,
});
return result.lines.join("\n");
}

describe("C# nullable reference types", () => {
test("keeps the default Newtonsoft output unchanged", async () => {
const output = await renderCSharp({
framework: "NewtonSoft",
"csharp-version": "6",
"check-required": true,
});

expect(output).toContain(
'[JsonProperty("profile", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]',
);
expect(output).toContain("public Profile Profile { get; set; }");
expect(output).toContain("public static Request FromJson(string json)");
expect(output).not.toContain("#nullable enable");
});

test("opts into nullable optional properties for Newtonsoft", async () => {
const output = await renderCSharp({
framework: "NewtonSoft",
"csharp-version": "6",
"check-required": true,
"nullable-reference-types": true,
});

expect(output).toContain("#nullable enable");
expect(output).toContain(
'[JsonProperty("profile", NullValueHandling = NullValueHandling.Ignore)]',
);
expect(output).not.toContain("Required.DisallowNull");
expect(output).toContain("public Profile? Profile { get; set; }");
expect(output).toContain("public string? DisplayName { get; set; }");
expect(output).toContain(
"public static Request? FromJson(string json)",
);
});
});
1 change: 1 addition & 0 deletions test/unit/renderer-options.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("rendererOptions typing", () => {
namespace: "Acme",
framework: "SystemTextJson",
"csharp-version": "6",
"nullable-reference-types": true,
},
});
});
Expand Down
Loading