@@ -516,6 +516,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
516516 IList < IOpenApiSchema > ? effectiveOneOf = OneOf ;
517517 IList < IOpenApiSchema > ? effectiveAnyOf = AnyOf ;
518518 bool hasNullInComposition = false ;
519+ bool hasOneOfNullAndSingleEnumWith3_0 = false ;
519520 JsonSchemaType ? inferredType = null ;
520521
521522 if ( version == OpenApiSpecVersion . OpenApi3_0 )
@@ -526,6 +527,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
526527 ( effectiveAnyOf , var inferredAnyOf , var nullInAnyOf ) = ProcessCompositionForNull ( AnyOf ) ;
527528 hasNullInComposition |= nullInAnyOf ;
528529 inferredType = inferredAnyOf ?? inferredType ;
530+
531+ hasOneOfNullAndSingleEnumWith3_0 = nullInOneOf && effectiveOneOf is { Count : 1 } &&
532+ effectiveOneOf [ 0 ] . Enum is { Count : > 0 } ;
529533 }
530534
531535 // type
@@ -538,7 +542,27 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
538542 writer . WriteOptionalCollection ( OpenApiConstants . AnyOf , effectiveAnyOf , callback ) ;
539543
540544 // oneOf
541- writer . WriteOptionalCollection ( OpenApiConstants . OneOf , effectiveOneOf , callback ) ;
545+ if ( hasOneOfNullAndSingleEnumWith3_0 )
546+ {
547+ writer . WriteRequiredCollection ( OpenApiConstants . OneOf , effectiveOneOf ! , ( writer , element ) =>
548+ {
549+ var clonedToMutateEnum = element . CreateShallowCopy ( ) ;
550+ if ( clonedToMutateEnum is OpenApiSchema { Enum : { } existingEnum } concreteCloned )
551+ {
552+ concreteCloned . Enum = [ .. existingEnum , JsonNullSentinel . JsonNull ] ;
553+ callback ( writer , clonedToMutateEnum ) ;
554+ }
555+ else
556+ {
557+ callback ( writer , element ) ;
558+ }
559+ } ) ;
560+ }
561+ else
562+ {
563+ writer . WriteOptionalCollection ( OpenApiConstants . OneOf , effectiveOneOf , callback ) ;
564+ }
565+
542566
543567 // not
544568 writer . WriteOptionalObject ( OpenApiConstants . Not , Not , callback ) ;
@@ -1065,10 +1089,32 @@ private static (IList<IOpenApiSchema>? effective, JsonSchemaType? inferredType,
10651089
10661090 foreach ( var schema in nonNullSchemas )
10671091 {
1068- commonType |= schema . Type . GetValueOrDefault ( ) & ~ JsonSchemaType . Null ;
1092+ if ( schema . Type . HasValue )
1093+ {
1094+ commonType |= schema . Type . Value & ~ JsonSchemaType . Null ;
1095+ }
1096+ else if ( schema . Enum is { Count : > 0 } )
1097+ {
1098+ foreach ( var enumValue in schema . Enum . Where ( x => x is not null ) )
1099+ {
1100+ var currentType = enumValue . GetValueKind ( ) switch
1101+ {
1102+ JsonValueKind . Array => JsonSchemaType . Array ,
1103+ JsonValueKind . String => JsonSchemaType . String ,
1104+ JsonValueKind . Number => JsonSchemaType . Number ,
1105+ JsonValueKind . True or JsonValueKind . False => JsonSchemaType . Boolean ,
1106+ JsonValueKind . Null => ( JsonSchemaType ) 0 ,
1107+ _ => JsonSchemaType . Object ,
1108+ } ;
1109+
1110+ commonType |= currentType ;
1111+ }
1112+
1113+ commonType |= JsonSchemaType . String ;
1114+ }
10691115 }
10701116
1071- return ( nonNullSchemas , commonType , true ) ;
1117+ return ( nonNullSchemas , commonType == 0 ? null : commonType , true ) ;
10721118 }
10731119 else
10741120 {
0 commit comments