@@ -4,97 +4,85 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
44
55internal static class OpenApiSchemaExtensions
66{
7- extension ( IOpenApiSchema schema )
7+ public static OpenApiSchema AsInlineSchema ( this IOpenApiSchema schema )
88 {
9- public OpenApiSchema AsInlineSchema ( )
10- {
11- ConsistencyGuard . ThrowIf ( schema is not OpenApiSchema ) ;
12- return ( OpenApiSchema ) schema ;
13- }
9+ ConsistencyGuard . ThrowIf ( schema is not OpenApiSchema ) ;
10+ return ( OpenApiSchema ) schema ;
11+ }
1412
15- public OpenApiSchemaReference AsReferenceSchema ( )
16- {
17- ConsistencyGuard . ThrowIf ( schema is not OpenApiSchemaReference ) ;
18- return ( OpenApiSchemaReference ) schema ;
19- }
13+ public static OpenApiSchemaReference AsReferenceSchema ( this IOpenApiSchema schema )
14+ {
15+ ConsistencyGuard . ThrowIf ( schema is not OpenApiSchemaReference ) ;
16+ return ( OpenApiSchemaReference ) schema ;
2017 }
2118
22- extension ( OpenApiSchemaReference referenceSchema )
19+ public static string GetReferenceId ( this OpenApiSchemaReference referenceSchema )
2320 {
24- public string GetReferenceId ( )
25- {
26- string ? schemaId = referenceSchema . Reference . Id ;
27- ConsistencyGuard . ThrowIf ( schemaId is null ) ;
28- return schemaId ;
29- }
21+ string ? schemaId = referenceSchema . Reference . Id ;
22+ ConsistencyGuard . ThrowIf ( schemaId is null ) ;
23+ return schemaId ;
3024 }
3125
32- extension ( OpenApiSchema inlineSchema )
26+ public static void SetNullable ( this OpenApiSchema inlineSchema , bool nullable )
3327 {
34- public void SetNullable ( bool nullable )
35- {
36- ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
28+ ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
3729
38- if ( nullable )
39- {
40- inlineSchema . Type ??= JsonSchemaType . Null ;
41- inlineSchema . Type |= JsonSchemaType . Null ;
42- }
43- else
30+ if ( nullable )
31+ {
32+ inlineSchema . Type ??= JsonSchemaType . Null ;
33+ inlineSchema . Type |= JsonSchemaType . Null ;
34+ }
35+ else
36+ {
37+ if ( inlineSchema . Type != null )
4438 {
45- if ( inlineSchema . Type != null )
46- {
47- inlineSchema . Type &= ~ JsonSchemaType . Null ;
48- }
39+ inlineSchema . Type &= ~ JsonSchemaType . Null ;
4940 }
5041 }
42+ }
43+
44+ public static void ReorderProperties ( this OpenApiSchema inlineSchema , IEnumerable < string > propertyNamesInOrder )
45+ {
46+ ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
47+ ArgumentNullException . ThrowIfNull ( propertyNamesInOrder ) ;
5148
52- public void ReorderProperties ( IEnumerable < string > propertyNamesInOrder )
49+ if ( inlineSchema . Properties is { Count : > 1 } )
5350 {
54- ArgumentNullException . ThrowIfNull ( inlineSchema ) ;
55- ArgumentNullException . ThrowIfNull ( propertyNamesInOrder ) ;
51+ var propertiesInOrder = new Dictionary < string , IOpenApiSchema > ( ) ;
5652
57- if ( inlineSchema . Properties is { Count : > 1 } )
53+ foreach ( string propertyName in propertyNamesInOrder )
5854 {
59- var propertiesInOrder = new Dictionary < string , IOpenApiSchema > ( ) ;
60-
61- foreach ( string propertyName in propertyNamesInOrder )
55+ if ( inlineSchema . Properties . TryGetValue ( propertyName , out IOpenApiSchema ? schema ) )
6256 {
63- if ( inlineSchema . Properties . TryGetValue ( propertyName , out IOpenApiSchema ? schema ) )
64- {
65- propertiesInOrder . Add ( propertyName , schema ) ;
66- }
57+ propertiesInOrder . Add ( propertyName , schema ) ;
6758 }
59+ }
6860
69- ConsistencyGuard . ThrowIf ( inlineSchema . Properties . Count != propertiesInOrder . Count ) ;
61+ ConsistencyGuard . ThrowIf ( inlineSchema . Properties . Count != propertiesInOrder . Count ) ;
7062
71- inlineSchema . Properties = propertiesInOrder ;
72- }
63+ inlineSchema . Properties = propertiesInOrder ;
7364 }
7465 }
7566
76- extension ( IOpenApiSchema source )
67+ public static OpenApiSchema WrapInExtendedSchema ( this IOpenApiSchema source )
7768 {
78- public OpenApiSchema WrapInExtendedSchema ( )
79- {
80- ArgumentNullException . ThrowIfNull ( source ) ;
81-
82- return new OpenApiSchema
83- {
84- AllOf = [ source ]
85- } ;
86- }
69+ ArgumentNullException . ThrowIfNull ( source ) ;
8770
88- public IOpenApiSchema UnwrapLastExtendedSchema ( )
71+ return new OpenApiSchema
8972 {
90- ArgumentNullException . ThrowIfNull ( source ) ;
73+ AllOf = [ source ]
74+ } ;
75+ }
9176
92- if ( source is OpenApiSchema && source . AllOf is { Count : > 0 } )
93- {
94- return source . AllOf . Last ( ) ;
95- }
77+ public static IOpenApiSchema UnwrapLastExtendedSchema ( this IOpenApiSchema source )
78+ {
79+ ArgumentNullException . ThrowIfNull ( source ) ;
9680
97- return source ;
81+ if ( source is OpenApiSchema && source . AllOf is { Count : > 0 } )
82+ {
83+ return source . AllOf . Last ( ) ;
9884 }
85+
86+ return source ;
9987 }
10088}
0 commit comments