1+ using System . Reflection ;
12using JsonApiDotNetCore . Configuration ;
3+ using JsonApiDotNetCore . Middleware ;
4+ using JsonApiDotNetCore . OpenApi . Swashbuckle . Annotations ;
25using Microsoft . OpenApi . Models ;
36using Swashbuckle . AspNetCore . SwaggerGen ;
47
@@ -7,32 +10,48 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators.Components;
710internal sealed class ResourceIdSchemaGenerator
811{
912 private readonly SchemaGenerator _defaultSchemaGenerator ;
13+ private readonly IControllerResourceMapping _controllerResourceMapping ;
1014
11- public ResourceIdSchemaGenerator ( SchemaGenerator defaultSchemaGenerator )
15+ public ResourceIdSchemaGenerator ( SchemaGenerator defaultSchemaGenerator , IControllerResourceMapping controllerResourceMapping )
1216 {
1317 ArgumentNullException . ThrowIfNull ( defaultSchemaGenerator ) ;
18+ ArgumentNullException . ThrowIfNull ( controllerResourceMapping ) ;
1419
1520 _defaultSchemaGenerator = defaultSchemaGenerator ;
21+ _controllerResourceMapping = controllerResourceMapping ;
1622 }
1723
18- public OpenApiSchema GenerateSchema ( ResourceType resourceType , SchemaRepository schemaRepository )
24+ public OpenApiSchema GenerateSchema ( ParameterInfo parameter , SchemaRepository schemaRepository )
1925 {
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 ) ;
2134
22- return GenerateSchema ( resourceType . IdentityClrType , schemaRepository ) ;
35+ return GenerateSchema ( resourceType , schemaRepository ) ;
2336 }
2437
25- public OpenApiSchema GenerateSchema ( Type resourceIdClrType , SchemaRepository schemaRepository )
38+ public OpenApiSchema GenerateSchema ( ResourceType resourceType , SchemaRepository schemaRepository )
2639 {
27- ArgumentNullException . ThrowIfNull ( resourceIdClrType ) ;
40+ ArgumentNullException . ThrowIfNull ( resourceType ) ;
2841 ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
2942
30- OpenApiSchema idSchema = _defaultSchemaGenerator . GenerateSchema ( resourceIdClrType , schemaRepository ) ;
43+ OpenApiSchema idSchema = _defaultSchemaGenerator . GenerateSchema ( resourceType . IdentityClrType , schemaRepository ) ;
3144 ConsistencyGuard . ThrowIf ( idSchema . Reference != null ) ;
3245
3346 idSchema . Type = "string" ;
3447
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 ) )
3655 {
3756 // When using string IDs, it's discouraged (but possible) to use an empty string as primary key value, because
3857 // some things won't work: get-by-id, update and delete resource are impossible, and rendered links are unusable.
0 commit comments