@@ -9,6 +9,8 @@ namespace JsonApiDotNetCore.OpenApi;
9
9
10
10
internal sealed class JsonApiSchemaIdSelector
11
11
{
12
+ private const string ResourceTypeSchemaIdTemplate = "[ResourceName] Resource Type" ;
13
+
12
14
private static readonly IDictionary < Type , string > OpenTypeToSchemaTemplateMap = new Dictionary < Type , string >
13
15
{
14
16
[ typeof ( ResourcePostRequestDocument < > ) ] = "[ResourceName] Post Request Document" ,
@@ -61,19 +63,37 @@ public string GetSchemaId(Type type)
61
63
return resourceType . PublicName . Singularize ( ) ;
62
64
}
63
65
64
- JsonNamingPolicy ? namingPolicy = _options . SerializerOptions . PropertyNamingPolicy ;
65
-
66
- if ( type . IsConstructedGenericType && OpenTypeToSchemaTemplateMap . ContainsKey ( type . GetGenericTypeDefinition ( ) ) )
66
+ if ( type . IsConstructedGenericType )
67
67
{
68
68
Type openType = type . GetGenericTypeDefinition ( ) ;
69
- Type resourceClrType = type . GetGenericArguments ( ) . First ( ) ;
70
- resourceType = _resourceGraph . GetResourceType ( resourceClrType ) ;
71
69
72
- string pascalCaseSchemaId = OpenTypeToSchemaTemplateMap [ openType ] . Replace ( "[ResourceName]" , resourceType . PublicName . Singularize ( ) ) . ToPascalCase ( ) ;
73
- return namingPolicy != null ? namingPolicy . ConvertName ( pascalCaseSchemaId ) : pascalCaseSchemaId ;
70
+ if ( OpenTypeToSchemaTemplateMap . TryGetValue ( openType , out string ? schemaTemplate ) )
71
+ {
72
+ Type resourceClrType = type . GetGenericArguments ( ) . First ( ) ;
73
+ resourceType = _resourceGraph . GetResourceType ( resourceClrType ) ;
74
+
75
+ return ApplySchemaTemplate ( schemaTemplate , resourceType ) ;
76
+ }
74
77
}
75
78
76
- // Used for a fixed set of types, such as JsonApiObject, LinksInResourceCollectionDocument etc.
77
- return namingPolicy != null ? namingPolicy . ConvertName ( type . Name ) : type . Name ;
79
+ // Used for a fixed set of non-generic types, such as Jsonapi, LinksInResourceCollectionDocument etc.
80
+ return ApplySchemaTemplate ( type . Name , null ) ;
81
+ }
82
+
83
+ private string ApplySchemaTemplate ( string schemaTemplate , ResourceType ? resourceType )
84
+ {
85
+ string pascalCaseSchemaId = resourceType != null
86
+ ? schemaTemplate . Replace ( "[ResourceName]" , resourceType . PublicName . Singularize ( ) ) . ToPascalCase ( )
87
+ : schemaTemplate . ToPascalCase ( ) ;
88
+
89
+ JsonNamingPolicy ? namingPolicy = _options . SerializerOptions . PropertyNamingPolicy ;
90
+ return namingPolicy != null ? namingPolicy . ConvertName ( pascalCaseSchemaId ) : pascalCaseSchemaId ;
91
+ }
92
+
93
+ public string GetSchemaId ( ResourceType resourceType )
94
+ {
95
+ ArgumentGuard . NotNull ( resourceType ) ;
96
+
97
+ return ApplySchemaTemplate ( ResourceTypeSchemaIdTemplate , resourceType ) ;
78
98
}
79
99
}
0 commit comments