diff --git a/packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts b/packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts index 09cfcc3ca..d07dea90f 100644 --- a/packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts +++ b/packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts @@ -125,7 +125,7 @@ export class CSharpRenderer extends ConvenienceRenderer { protected nullableCSType(t: Type, follow: (t: Type) => Type = followTargetType, withIssues = false): 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; diff --git a/packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts b/packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts index 77afed6da..dfede9be4 100644 --- a/packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts +++ b/packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts @@ -217,10 +217,13 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer { const isNullable = followTargetType(property.type).isNullable; const isOptional = property.isOptional; - if (isOptional && !isNullable) { + if (!isOptional) { + attributes.push(["[", "JsonRequired", "]"]); + } else if (isOptional && !isNullable) { attributes.push(["[", "JsonIgnore", "(Condition = JsonIgnoreCondition.WhenWritingNull)]"]); } + // const requiredClass = this._options.dense ? "R" : "Required"; // const nullValueHandlingClass = this._options.dense ? "N" : "NullValueHandling"; // const nullValueHandling = isOptional && !isNullable ? [", NullValueHandling = ", nullValueHandlingClass, ".Ignore"] : []; diff --git a/packages/quicktype-core/src/language/CSharp/language.ts b/packages/quicktype-core/src/language/CSharp/language.ts index e2b460582..123147d4c 100644 --- a/packages/quicktype-core/src/language/CSharp/language.ts +++ b/packages/quicktype-core/src/language/CSharp/language.ts @@ -16,7 +16,7 @@ export enum Framework { SystemTextJson = "SystemTextJson" } -export type Version = 5 | 6; +export type Version = 5 | 6 | 8; export interface OutputFeatures { attributes: boolean; helpers: boolean; @@ -55,7 +55,8 @@ export const cSharpOptions = { "C# version", [ ["5", 5], - ["6", 6] + ["6", 6], + ["8", 8] ], "6", "secondary"