@@ -269,13 +269,15 @@ function generateDataClass(
269269 enumOutput
270270 ) ;
271271
272+ const isNullableType = csharpType . endsWith ( "?" ) ;
272273 if ( ! isRequired ) {
273274 lines . push (
274275 `${ indent } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]`
275276 ) ;
276277 }
277278 lines . push ( `${ indent } [JsonPropertyName("${ propName } ")]` ) ;
278- const requiredModifier = isRequired ? "required " : "" ;
279+
280+ const requiredModifier = isRequired && ! isNullableType ? "required " : "" ;
279281 lines . push ( `${ indent } public ${ requiredModifier } ${ csharpType } ${ csharpName } { get; set; }` ) ;
280282 lines . push ( "" ) ;
281283 }
@@ -330,7 +332,9 @@ function generateNestedClass(
330332 ) ;
331333 }
332334 lines . push ( `${ indent } [JsonPropertyName("${ propName } ")]` ) ;
333- const requiredModifier = isRequired ? "required " : "" ;
335+
336+ const isNullableType = csharpType . endsWith ( "?" ) ;
337+ const requiredModifier = isRequired && ! isNullableType ? "required " : "" ;
334338 lines . push ( `${ indent } public ${ requiredModifier } ${ csharpType } ${ csharpName } { get; set; }` ) ;
335339 lines . push ( "" ) ;
336340 }
@@ -361,24 +365,27 @@ function resolvePropertyType(
361365) : string {
362366 // Handle anyOf - simplify to nullable of the non-null type or object
363367 if ( propSchema . anyOf ) {
368+ const hasNull = propSchema . anyOf . some (
369+ ( s ) => typeof s === "object" && ( s as JSONSchema7 ) . type === "null"
370+ ) ;
364371 const nonNullTypes = propSchema . anyOf . filter (
365372 ( s ) => typeof s === "object" && ( s as JSONSchema7 ) . type !== "null"
366373 ) ;
367374 if ( nonNullTypes . length === 1 ) {
368- // Simple nullable - recurse with the inner type
375+ // Simple nullable - recurse with the inner type, marking as not required if null is an option
369376 return resolvePropertyType (
370377 nonNullTypes [ 0 ] as JSONSchema7 ,
371378 parentClassName ,
372379 propName ,
373- false ,
380+ isRequired && ! hasNull ,
374381 indent ,
375382 knownTypes ,
376383 nestedClasses ,
377384 enumOutput
378385 ) ;
379386 }
380- // Complex union - use object
381- return "object" ;
387+ // Complex union - use object, nullable if null is in the union or property is not required
388+ return ( hasNull || ! isRequired ) ? "object?" : "object" ;
382389 }
383390
384391 // Handle enum types
0 commit comments