1
+ using System . Reflection ;
1
2
using JsonApiDotNetCore . Configuration ;
3
+ using JsonApiDotNetCore . Middleware ;
4
+ using JsonApiDotNetCore . OpenApi . Swashbuckle . Annotations ;
2
5
using Microsoft . OpenApi . Models ;
3
6
using Swashbuckle . AspNetCore . SwaggerGen ;
4
7
@@ -7,32 +10,48 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Components;
7
10
internal sealed class ResourceIdSchemaGenerator
8
11
{
9
12
private readonly SchemaGenerator _defaultSchemaGenerator ;
13
+ private readonly IControllerResourceMapping _controllerResourceMapping ;
10
14
11
- public ResourceIdSchemaGenerator ( SchemaGenerator defaultSchemaGenerator )
15
+ public ResourceIdSchemaGenerator ( SchemaGenerator defaultSchemaGenerator , IControllerResourceMapping controllerResourceMapping )
12
16
{
13
17
ArgumentNullException . ThrowIfNull ( defaultSchemaGenerator ) ;
18
+ ArgumentNullException . ThrowIfNull ( controllerResourceMapping ) ;
14
19
15
20
_defaultSchemaGenerator = defaultSchemaGenerator ;
21
+ _controllerResourceMapping = controllerResourceMapping ;
16
22
}
17
23
18
- public OpenApiSchema GenerateSchema ( ResourceType resourceType , SchemaRepository schemaRepository )
24
+ public OpenApiSchema GenerateSchema ( ParameterInfo parameter , SchemaRepository schemaRepository )
19
25
{
20
- ArgumentNullException . ThrowIfNull ( resourceType ) ;
26
+ ArgumentNullException . ThrowIfNull ( parameter ) ;
27
+ ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
28
+
29
+ Type ? controllerType = parameter . Member . ReflectedType ;
30
+ ConsistencyGuard . ThrowIf ( controllerType == null ) ;
31
+
32
+ ResourceType ? resourceType = _controllerResourceMapping . GetResourceTypeForController ( controllerType ) ;
33
+ ConsistencyGuard . ThrowIf ( resourceType == null ) ;
21
34
22
- return GenerateSchema ( resourceType . IdentityClrType , schemaRepository ) ;
35
+ return GenerateSchema ( resourceType , schemaRepository ) ;
23
36
}
24
37
25
- public OpenApiSchema GenerateSchema ( Type resourceIdClrType , SchemaRepository schemaRepository )
38
+ public OpenApiSchema GenerateSchema ( ResourceType resourceType , SchemaRepository schemaRepository )
26
39
{
27
- ArgumentNullException . ThrowIfNull ( resourceIdClrType ) ;
40
+ ArgumentNullException . ThrowIfNull ( resourceType ) ;
28
41
ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
29
42
30
- OpenApiSchema idSchema = _defaultSchemaGenerator . GenerateSchema ( resourceIdClrType , schemaRepository ) ;
43
+ OpenApiSchema idSchema = _defaultSchemaGenerator . GenerateSchema ( resourceType . IdentityClrType , schemaRepository ) ;
31
44
ConsistencyGuard . ThrowIf ( idSchema . Reference != null ) ;
32
45
33
46
idSchema . Type = "string" ;
34
47
35
- if ( resourceIdClrType != typeof ( string ) )
48
+ var hideIdTypeAttribute = resourceType . ClrType . GetCustomAttribute < HideResourceIdTypeInOpenApiAttribute > ( ) ;
49
+
50
+ if ( hideIdTypeAttribute != null )
51
+ {
52
+ idSchema . Format = null ;
53
+ }
54
+ else if ( resourceType . IdentityClrType != typeof ( string ) )
36
55
{
37
56
// When using string IDs, it's discouraged (but possible) to use an empty string as primary key value, because
38
57
// some things won't work: get-by-id, update and delete resource are impossible, and rendered links are unusable.
0 commit comments