@@ -13,7 +13,7 @@ internal sealed class ResourceFieldSchemaBuilder
13
13
private readonly SchemaGenerator _defaultSchemaGenerator ;
14
14
private readonly ResourceIdentifierSchemaGenerator _resourceIdentifierSchemaGenerator ;
15
15
private readonly LinksVisibilitySchemaGenerator _linksVisibilitySchemaGenerator ;
16
- private readonly ResourceTypeInfo _resourceTypeInfo ;
16
+ private readonly ResourceSchemaType _resourceSchemaType ;
17
17
private readonly ResourceFieldValidationMetadataProvider _resourceFieldValidationMetadataProvider ;
18
18
private readonly RelationshipTypeFactory _relationshipTypeFactory ;
19
19
@@ -23,19 +23,19 @@ internal sealed class ResourceFieldSchemaBuilder
23
23
24
24
public ResourceFieldSchemaBuilder ( SchemaGenerator defaultSchemaGenerator , ResourceIdentifierSchemaGenerator resourceIdentifierSchemaGenerator ,
25
25
LinksVisibilitySchemaGenerator linksVisibilitySchemaGenerator , ResourceFieldValidationMetadataProvider resourceFieldValidationMetadataProvider ,
26
- RelationshipTypeFactory relationshipTypeFactory , ResourceTypeInfo resourceTypeInfo )
26
+ RelationshipTypeFactory relationshipTypeFactory , ResourceSchemaType resourceSchemaType )
27
27
{
28
28
ArgumentGuard . NotNull ( defaultSchemaGenerator ) ;
29
29
ArgumentGuard . NotNull ( resourceIdentifierSchemaGenerator ) ;
30
30
ArgumentGuard . NotNull ( linksVisibilitySchemaGenerator ) ;
31
- ArgumentGuard . NotNull ( resourceTypeInfo ) ;
31
+ ArgumentGuard . NotNull ( resourceSchemaType ) ;
32
32
ArgumentGuard . NotNull ( resourceFieldValidationMetadataProvider ) ;
33
33
ArgumentGuard . NotNull ( relationshipTypeFactory ) ;
34
34
35
35
_defaultSchemaGenerator = defaultSchemaGenerator ;
36
36
_resourceIdentifierSchemaGenerator = resourceIdentifierSchemaGenerator ;
37
37
_linksVisibilitySchemaGenerator = linksVisibilitySchemaGenerator ;
38
- _resourceTypeInfo = resourceTypeInfo ;
38
+ _resourceSchemaType = resourceSchemaType ;
39
39
_resourceFieldValidationMetadataProvider = resourceFieldValidationMetadataProvider ;
40
40
_relationshipTypeFactory = relationshipTypeFactory ;
41
41
@@ -44,9 +44,9 @@ public ResourceFieldSchemaBuilder(SchemaGenerator defaultSchemaGenerator, Resour
44
44
45
45
private IDictionary < string , OpenApiSchema > GetFieldSchemas ( )
46
46
{
47
- if ( ! _resourceSchemaRepository . TryLookupByType ( _resourceTypeInfo . ResourceType . ClrType , out OpenApiSchema referenceSchemaForResource ) )
47
+ if ( ! _resourceSchemaRepository . TryLookupByType ( _resourceSchemaType . ResourceType . ClrType , out OpenApiSchema referenceSchemaForResource ) )
48
48
{
49
- referenceSchemaForResource = _defaultSchemaGenerator . GenerateSchema ( _resourceTypeInfo . ResourceType . ClrType , _resourceSchemaRepository ) ;
49
+ referenceSchemaForResource = _defaultSchemaGenerator . GenerateSchema ( _resourceSchemaType . ResourceType . ClrType , _resourceSchemaRepository ) ;
50
50
}
51
51
52
52
OpenApiSchema fullSchemaForResource = _resourceSchemaRepository . Schemas [ referenceSchemaForResource . Reference . Id ] ;
@@ -57,12 +57,13 @@ public void SetMembersOfAttributes(OpenApiSchema fullSchemaForAttributes, bool f
57
57
{
58
58
ArgumentGuard . NotNull ( fullSchemaForAttributes ) ;
59
59
ArgumentGuard . NotNull ( schemaRepository ) ;
60
+ AssertHasNoProperties ( fullSchemaForAttributes ) ;
60
61
61
- AttrCapabilities requiredCapability = GetRequiredCapabilityForAttributes ( _resourceTypeInfo . ResourceDataOpenType ) ;
62
+ AttrCapabilities requiredCapability = GetRequiredCapabilityForAttributes ( _resourceSchemaType . SchemaOpenType ) ;
62
63
63
- foreach ( ( string fieldName , OpenApiSchema resourceFieldSchema ) in _schemasForResourceFields )
64
+ foreach ( ( string publicName , OpenApiSchema schemaForResourceField ) in _schemasForResourceFields )
64
65
{
65
- AttrAttribute ? matchingAttribute = _resourceTypeInfo . ResourceType . FindAttributeByPublicName ( fieldName ) ;
66
+ AttrAttribute ? matchingAttribute = _resourceSchemaType . ResourceType . FindAttributeByPublicName ( publicName ) ;
66
67
67
68
if ( matchingAttribute != null && matchingAttribute . Capabilities . HasFlag ( requiredCapability ) )
68
69
{
@@ -81,24 +82,25 @@ public void SetMembersOfAttributes(OpenApiSchema fullSchemaForAttributes, bool f
81
82
}
82
83
}
83
84
84
- bool isInlineSchemaType = resourceFieldSchema . AllOf . Count == 0 ;
85
+ bool isInlineSchemaType = schemaForResourceField . AllOf . Count == 0 ;
85
86
86
87
// Schemas for types like enum and complex attributes are handled as reference schemas.
87
88
if ( ! isInlineSchemaType )
88
89
{
89
- EnsureAttributeSchemaIsExposed ( resourceFieldSchema . UnwrapLastExtendedSchema ( ) , matchingAttribute , schemaRepository ) ;
90
+ OpenApiSchema referenceSchemaForAttribute = schemaForResourceField . UnwrapLastExtendedSchema ( ) ;
91
+ EnsureAttributeSchemaIsExposed ( referenceSchemaForAttribute , matchingAttribute , schemaRepository ) ;
90
92
}
91
93
92
- fullSchemaForAttributes . Properties . Add ( matchingAttribute . PublicName , resourceFieldSchema ) ;
94
+ fullSchemaForAttributes . Properties . Add ( matchingAttribute . PublicName , schemaForResourceField ) ;
93
95
94
- resourceFieldSchema . Nullable = _resourceFieldValidationMetadataProvider . IsNullable ( matchingAttribute ) ;
96
+ schemaForResourceField . Nullable = _resourceFieldValidationMetadataProvider . IsNullable ( matchingAttribute ) ;
95
97
96
98
if ( IsFieldRequired ( matchingAttribute ) )
97
99
{
98
100
fullSchemaForAttributes . Required . Add ( matchingAttribute . PublicName ) ;
99
101
}
100
102
101
- resourceFieldSchema . Description = _resourceDocumentationReader . GetDocumentationForAttribute ( matchingAttribute ) ;
103
+ schemaForResourceField . Description = _resourceDocumentationReader . GetDocumentationForAttribute ( matchingAttribute ) ;
102
104
}
103
105
}
104
106
}
@@ -142,18 +144,19 @@ private Type GetRepresentedTypeForAttributeSchema(AttrAttribute attribute)
142
144
143
145
private bool IsFieldRequired ( ResourceFieldAttribute field )
144
146
{
145
- bool isCreateResourceSchemaType = _resourceTypeInfo . ResourceDataOpenType == typeof ( DataInCreateResourceRequest < > ) ;
147
+ bool isCreateResourceSchemaType = _resourceSchemaType . SchemaOpenType == typeof ( DataInCreateResourceRequest < > ) ;
146
148
return isCreateResourceSchemaType && _resourceFieldValidationMetadataProvider . IsRequired ( field ) ;
147
149
}
148
150
149
151
public void SetMembersOfRelationships ( OpenApiSchema fullSchemaForRelationships , bool forRequestSchema , SchemaRepository schemaRepository )
150
152
{
151
153
ArgumentGuard . NotNull ( fullSchemaForRelationships ) ;
152
154
ArgumentGuard . NotNull ( schemaRepository ) ;
155
+ AssertHasNoProperties ( fullSchemaForRelationships ) ;
153
156
154
- foreach ( string fieldName in _schemasForResourceFields . Keys )
157
+ foreach ( string publicName in _schemasForResourceFields . Keys )
155
158
{
156
- RelationshipAttribute ? matchingRelationship = _resourceTypeInfo . ResourceType . FindRelationshipByPublicName ( fieldName ) ;
159
+ RelationshipAttribute ? matchingRelationship = _resourceSchemaType . ResourceType . FindRelationshipByPublicName ( publicName ) ;
157
160
158
161
if ( matchingRelationship != null )
159
162
{
@@ -166,7 +169,7 @@ public void SetMembersOfRelationships(OpenApiSchema fullSchemaForRelationships,
166
169
private void AddRelationshipSchemaToResourceData ( RelationshipAttribute relationship , OpenApiSchema fullSchemaForRelationships ,
167
170
SchemaRepository schemaRepository )
168
171
{
169
- Type relationshipSchemaType = GetRelationshipSchemaType ( relationship , _resourceTypeInfo . ResourceDataOpenType ) ;
172
+ Type relationshipSchemaType = GetRelationshipSchemaType ( relationship , _resourceSchemaType . SchemaOpenType ) ;
170
173
171
174
OpenApiSchema referenceSchemaForRelationship = GetReferenceSchemaForRelationship ( relationshipSchemaType , schemaRepository ) ??
172
175
CreateReferenceSchemaForRelationship ( relationshipSchemaType , schemaRepository ) ;
@@ -182,9 +185,9 @@ private void AddRelationshipSchemaToResourceData(RelationshipAttribute relations
182
185
}
183
186
}
184
187
185
- private Type GetRelationshipSchemaType ( RelationshipAttribute relationship , Type resourceDataConstructedType )
188
+ private Type GetRelationshipSchemaType ( RelationshipAttribute relationship , Type openSchemaType )
186
189
{
187
- bool isResponseSchemaType = resourceDataConstructedType . ConstructedToOpenType ( ) . IsAssignableTo ( typeof ( ResourceDataInResponse < > ) ) ;
190
+ bool isResponseSchemaType = openSchemaType . IsAssignableTo ( typeof ( ResourceDataInResponse < > ) ) ;
188
191
return isResponseSchemaType ? _relationshipTypeFactory . GetForResponse ( relationship ) : _relationshipTypeFactory . GetForRequest ( relationship ) ;
189
192
}
190
193
@@ -211,4 +214,12 @@ private OpenApiSchema CreateReferenceSchemaForRelationship(Type relationshipSche
211
214
212
215
return referenceSchema ;
213
216
}
217
+
218
+ private static void AssertHasNoProperties ( OpenApiSchema fullSchema )
219
+ {
220
+ if ( fullSchema . Properties . Count > 0 )
221
+ {
222
+ throw new UnreachableCodeException ( ) ;
223
+ }
224
+ }
214
225
}
0 commit comments