@@ -4,85 +4,97 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
44
55internal static class OpenApiSchemaExtensions
66{
7- public static OpenApiSchema AsInlineSchema ( this IOpenApiSchema schema )
7+ extension ( IOpenApiSchema schema )
88 {
9- ConsistencyGuard . ThrowIf ( schema is not OpenApiSchema ) ;
10- return ( OpenApiSchema ) schema ;
11- }
9+ public OpenApiSchema AsInlineSchema ( )
10+ {
11+ ConsistencyGuard . ThrowIf ( schema is not OpenApiSchema ) ;
12+ return ( OpenApiSchema ) schema ;
13+ }
1214
13- public static OpenApiSchemaReference AsReferenceSchema ( this IOpenApiSchema schema )
14- {
15- ConsistencyGuard . ThrowIf ( schema is not OpenApiSchemaReference ) ;
16- return ( OpenApiSchemaReference ) schema ;
15+ public OpenApiSchemaReference AsReferenceSchema ( )
16+ {
17+ ConsistencyGuard . ThrowIf ( schema is not OpenApiSchemaReference ) ;
18+ return ( OpenApiSchemaReference ) schema ;
19+ }
1720 }
1821
19- public static string GetReferenceId ( this OpenApiSchemaReference referenceSchema )
22+ extension ( OpenApiSchemaReference referenceSchema )
2023 {
21- string ? schemaId = referenceSchema . Reference . Id ;
22- ConsistencyGuard . ThrowIf ( schemaId is null ) ;
23- return schemaId ;
24+ public string GetReferenceId ( )
25+ {
26+ string ? schemaId = referenceSchema . Reference . Id ;
27+ ConsistencyGuard . ThrowIf ( schemaId is null ) ;
28+ return schemaId ;
29+ }
2430 }
2531
26- public static void SetNullable ( this OpenApiSchema inlineSchema , bool nullable )
32+ extension ( OpenApiSchema inlineSchema )
2733 {
28- ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
29-
30- if ( nullable )
31- {
32- inlineSchema . Type ??= JsonSchemaType . Null ;
33- inlineSchema . Type |= JsonSchemaType . Null ;
34- }
35- else
34+ public void SetNullable ( bool nullable )
3635 {
37- if ( inlineSchema . Type != null )
36+ ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
37+
38+ if ( nullable )
3839 {
39- inlineSchema . Type &= ~ JsonSchemaType . Null ;
40+ inlineSchema . Type ??= JsonSchemaType . Null ;
41+ inlineSchema . Type |= JsonSchemaType . Null ;
42+ }
43+ else
44+ {
45+ if ( inlineSchema . Type != null )
46+ {
47+ inlineSchema . Type &= ~ JsonSchemaType . Null ;
48+ }
4049 }
4150 }
42- }
43-
44- public static void ReorderProperties ( this OpenApiSchema inlineSchema , IEnumerable < string > propertyNamesInOrder )
45- {
46- ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
47- ArgumentNullException . ThrowIfNull ( propertyNamesInOrder ) ;
4851
49- if ( inlineSchema . Properties is { Count : > 1 } )
52+ public void ReorderProperties ( IEnumerable < string > propertyNamesInOrder )
5053 {
51- var propertiesInOrder = new Dictionary < string , IOpenApiSchema > ( ) ;
54+ ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
55+ ArgumentNullException . ThrowIfNull ( propertyNamesInOrder ) ;
5256
53- foreach ( string propertyName in propertyNamesInOrder )
57+ if ( inlineSchema . Properties is { Count : > 1 } )
5458 {
55- if ( inlineSchema . Properties . TryGetValue ( propertyName , out IOpenApiSchema ? schema ) )
59+ var propertiesInOrder = new Dictionary < string , IOpenApiSchema > ( ) ;
60+
61+ foreach ( string propertyName in propertyNamesInOrder )
5662 {
57- propertiesInOrder . Add ( propertyName , schema ) ;
63+ if ( inlineSchema . Properties . TryGetValue ( propertyName , out IOpenApiSchema ? schema ) )
64+ {
65+ propertiesInOrder . Add ( propertyName , schema ) ;
66+ }
5867 }
59- }
6068
61- ConsistencyGuard . ThrowIf ( inlineSchema . Properties . Count != propertiesInOrder . Count ) ;
69+ ConsistencyGuard . ThrowIf ( inlineSchema . Properties . Count != propertiesInOrder . Count ) ;
6270
63- inlineSchema . Properties = propertiesInOrder ;
71+ inlineSchema . Properties = propertiesInOrder ;
72+ }
6473 }
6574 }
6675
67- public static OpenApiSchema WrapInExtendedSchema ( this IOpenApiSchema source )
76+ extension ( IOpenApiSchema source )
6877 {
69- ArgumentNullException . ThrowIfNull ( source ) ;
70-
71- return new OpenApiSchema
78+ public OpenApiSchema WrapInExtendedSchema ( )
7279 {
73- AllOf = [ source ]
74- } ;
75- }
80+ ArgumentNullException . ThrowIfNull ( source ) ;
7681
77- public static IOpenApiSchema UnwrapLastExtendedSchema ( this IOpenApiSchema source )
78- {
79- ArgumentNullException . ThrowIfNull ( source ) ;
82+ return new OpenApiSchema
83+ {
84+ AllOf = [ source ]
85+ } ;
86+ }
8087
81- if ( source is OpenApiSchema && source . AllOf is { Count : > 0 } )
88+ public IOpenApiSchema UnwrapLastExtendedSchema ( )
8289 {
83- return source . AllOf . Last ( ) ;
84- }
90+ ArgumentNullException . ThrowIfNull ( source ) ;
8591
86- return source ;
92+ if ( source is OpenApiSchema && source . AllOf is { Count : > 0 } )
93+ {
94+ return source . AllOf . Last ( ) ;
95+ }
96+
97+ return source ;
98+ }
8799 }
88100}
0 commit comments