@@ -521,6 +521,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
521521 IList < IOpenApiSchema > ? effectiveOneOf = OneOf ;
522522 IList < IOpenApiSchema > ? effectiveAnyOf = AnyOf ;
523523 bool hasNullInComposition = false ;
524+ bool hasOneOfNullAndSingleEnumWith3_0 = false ;
524525 JsonSchemaType ? inferredType = null ;
525526
526527 if ( version == OpenApiSpecVersion . OpenApi3_0 )
@@ -531,6 +532,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
531532 ( effectiveAnyOf , var inferredAnyOf , var nullInAnyOf ) = ProcessCompositionForNull ( AnyOf ) ;
532533 hasNullInComposition |= nullInAnyOf ;
533534 inferredType = inferredAnyOf ?? inferredType ;
535+
536+ hasOneOfNullAndSingleEnumWith3_0 = nullInOneOf && effectiveOneOf is { Count : 1 } &&
537+ effectiveOneOf [ 0 ] . Enum is { Count : > 0 } ;
534538 }
535539
536540 // type
@@ -543,7 +547,27 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
543547 writer . WriteOptionalCollection ( OpenApiConstants . AnyOf , effectiveAnyOf , callback ) ;
544548
545549 // oneOf
546- writer . WriteOptionalCollection ( OpenApiConstants . OneOf , effectiveOneOf , callback ) ;
550+ if ( hasOneOfNullAndSingleEnumWith3_0 )
551+ {
552+ writer . WriteRequiredCollection ( OpenApiConstants . OneOf , effectiveOneOf ! , ( writer , element ) =>
553+ {
554+ var clonedToMutateEnum = element . CreateShallowCopy ( ) ;
555+ if ( clonedToMutateEnum is OpenApiSchema { Enum : { } existingEnum } concreteCloned )
556+ {
557+ concreteCloned . Enum = [ .. existingEnum , JsonNullSentinel . JsonNull ] ;
558+ callback ( writer , clonedToMutateEnum ) ;
559+ }
560+ else
561+ {
562+ callback ( writer , element ) ;
563+ }
564+ } ) ;
565+ }
566+ else
567+ {
568+ writer . WriteOptionalCollection ( OpenApiConstants . OneOf , effectiveOneOf , callback ) ;
569+ }
570+
547571
548572 // not
549573 writer . WriteOptionalObject ( OpenApiConstants . Not , Not , callback ) ;
@@ -1070,10 +1094,32 @@ private static (IList<IOpenApiSchema>? effective, JsonSchemaType? inferredType,
10701094
10711095 foreach ( var schema in nonNullSchemas )
10721096 {
1073- commonType |= schema . Type . GetValueOrDefault ( ) & ~ JsonSchemaType . Null ;
1097+ if ( schema . Type . HasValue )
1098+ {
1099+ commonType |= schema . Type . Value & ~ JsonSchemaType . Null ;
1100+ }
1101+ else if ( schema . Enum is { Count : > 0 } )
1102+ {
1103+ foreach ( var enumValue in schema . Enum . Where ( x => x is not null ) )
1104+ {
1105+ var currentType = enumValue . GetValueKind ( ) switch
1106+ {
1107+ JsonValueKind . Array => JsonSchemaType . Array ,
1108+ JsonValueKind . String => JsonSchemaType . String ,
1109+ JsonValueKind . Number => JsonSchemaType . Number ,
1110+ JsonValueKind . True or JsonValueKind . False => JsonSchemaType . Boolean ,
1111+ JsonValueKind . Null => ( JsonSchemaType ) 0 ,
1112+ _ => JsonSchemaType . Object ,
1113+ } ;
1114+
1115+ commonType |= currentType ;
1116+ }
1117+
1118+ commonType |= JsonSchemaType . String ;
1119+ }
10741120 }
10751121
1076- return ( nonNullSchemas , commonType , true ) ;
1122+ return ( nonNullSchemas , commonType == 0 ? null : commonType , true ) ;
10771123 }
10781124 else
10791125 {
0 commit comments