Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
if: ${{ contains(matrix.fixture, 'csharp') }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6
dotnet-version: 8

- name: Install Rust
if: ${{ contains(matrix.fixture, 'rust') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class CSharpRenderer extends ConvenienceRenderer {
): Sourcelike {
t = followTargetType(t);
const csType = this.csType(t, follow, withIssues);
if (isValueType(t)) {
if (isValueType(t) || this._csOptions.version >= 8) {
return [csType, "?"];
} else {
return csType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
return this._options.baseclass;
}

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

this.emitLine("#pragma warning restore CS8618");
this.emitLine("#pragma warning restore CS8601");
this.emitLine("#pragma warning restore CS8603");
this.emitLine("#pragma warning restore CS8765");
}

protected emitDefaultLeadingComments(): void {
if (!this._needHelpers) return;

Expand Down Expand Up @@ -225,6 +234,14 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
";",
);
});

if (this._options.version >= 8) {
this.emitLine("#nullable enable");
this.emitLine("#pragma warning disable CS8618");
this.emitLine("#pragma warning disable CS8601");
this.emitLine("#pragma warning disable CS8603");
this.emitLine("#pragma warning disable CS8765");
}
}

private converterForType(t: Type): Name | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {
const isNullable = followTargetType(property.type).isNullable;
const isOptional = property.isOptional;

// [JsonRequired] makes deserialization fail if the property is
// missing from the JSON. It requires System.Text.Json 7.0 or
// later (.NET 7+).
if (this._options.checkRequired && !isOptional) {
attributes.push(["[JsonRequired]"]);
}

if (isOptional && !isNullable) {
attributes.push([
"[",
Expand Down
1 change: 1 addition & 0 deletions packages/quicktype-core/src/language/CSharp/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const cSharpOptions = {
{
"5": 5,
"6": 6,
"8": 8,
} as const,
"6",
"secondary",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/csharp-SystemTextJson/test.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="6.0.10" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion test/fixtures/csharp/test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
5 changes: 2 additions & 3 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const CSharpLanguage: Language = {
quickTestRendererOptions: [
{ "array-type": "list" },
{ "csharp-version": "5" },
{ "csharp-version": "8" },
{ density: "dense" },
{ "number-type": "decimal" },
{ "any-type": "dynamic" },
Expand Down Expand Up @@ -160,14 +161,12 @@ export const CSharpLanguageSystemTextJson: Language = {
"minmaxlength.schema", // generated converter triggers CS8602 warnings, which "dotnet run" prints to stdout, breaking the JSON comparison
"optional-constraints.schema", // same CS8602 stdout issue; also min/max on integers and pattern on optional strings aren't checked, so expected-failure samples don't fail
"optional-const-ref.schema", // same CS8602 stdout issue; also min/max on integers isn't checked, so the expected-failure sample doesn't fail
"required.schema", // the renderer doesn't implement check-required, so the expected-failure sample doesn't fail
"strict-optional.schema", // the renderer doesn't implement check-required, so the expected-failure sample doesn't fail
"intersection.schema", // the renderer doesn't implement check-required, so the expected-failure sample doesn't fail
],
rendererOptions: { "check-required": "true", framework: "SystemTextJson" },
quickTestRendererOptions: [
{ "array-type": "list" },
{ "csharp-version": "6" },
{ "csharp-version": "8" },
{ density: "dense" },
{ "number-type": "decimal" },
{ "any-type": "dynamic" },
Expand Down
Loading