11using System . Diagnostics . CodeAnalysis ;
2- using System . Reflection ;
32using Microsoft . OpenApi ;
43using Swashbuckle . AspNetCore . SwaggerGen ;
54
65namespace JsonApiDotNetCore . OpenApi . Swashbuckle ;
76
87internal static class SchemaRepositoryExtensions
98{
10- private const string ReservedIdsFieldName = "_reservedIds" ;
11- private static readonly FieldInfo ReservedIdsField = GetReservedIdsField ( ) ;
12-
13- private static FieldInfo GetReservedIdsField ( )
14- {
15- FieldInfo ? field = typeof ( SchemaRepository ) . GetField ( ReservedIdsFieldName , BindingFlags . Instance | BindingFlags . NonPublic ) ;
16-
17- if ( field == null )
18- {
19- throw new InvalidOperationException ( $ "Failed to locate private field '{ ReservedIdsFieldName } ' " +
20- $ "in type '{ typeof ( SchemaRepository ) . FullName } ' in assembly '{ typeof ( SchemaRepository ) . Assembly . FullName } '.") ;
21- }
22-
23- if ( field . FieldType != typeof ( Dictionary < Type , string > ) )
24- {
25- throw new InvalidOperationException ( $ "Unexpected type '{ field . FieldType } ' of private field '{ ReservedIdsFieldName } ' " +
26- $ "in type '{ typeof ( SchemaRepository ) . FullName } ' in assembly '{ typeof ( SchemaRepository ) . Assembly . FullName } '.") ;
27- }
28-
29- return field ;
30- }
31-
329 public static OpenApiSchemaReference LookupByType ( this SchemaRepository schemaRepository , Type schemaType )
3310 {
3411 ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
@@ -48,24 +25,4 @@ public static bool TryLookupByTypeSafe(this SchemaRepository schemaRepository, T
4825 referenceSchema = result ? obliviousReferenceSchema : null ;
4926 return result ;
5027 }
51-
52- public static void ReplaceSchemaId ( this SchemaRepository schemaRepository , Type oldSchemaType , string newSchemaId )
53- {
54- ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
55- ArgumentNullException . ThrowIfNull ( oldSchemaType ) ;
56- ArgumentException . ThrowIfNullOrEmpty ( newSchemaId ) ;
57-
58- if ( schemaRepository . TryLookupByTypeSafe ( oldSchemaType , out OpenApiSchemaReference ? referenceSchema ) )
59- {
60- string oldSchemaId = referenceSchema . GetReferenceId ( ) ;
61-
62- IOpenApiSchema targetSchema = schemaRepository . Schemas [ oldSchemaId ] ;
63-
64- schemaRepository . Schemas . Remove ( oldSchemaId ) ;
65- schemaRepository . Schemas . Add ( newSchemaId , targetSchema ) ;
66-
67- var reservedIds = ( Dictionary < Type , string > ) ReservedIdsField . GetValue ( schemaRepository ) ! ;
68- reservedIds . Remove ( oldSchemaType ) ;
69- }
70- }
7128}
0 commit comments