|
| 1 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents; |
| 2 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Relationships; |
| 3 | + |
| 4 | +#pragma warning disable AV1008 // Class should not be static |
| 5 | + |
| 6 | +namespace JsonApiDotNetCore.OpenApi.JsonApiObjects; |
| 7 | + |
| 8 | +internal static class JsonApiSchemaFacts |
| 9 | +{ |
| 10 | + private static readonly Type[] RequestSchemaTypes = |
| 11 | + [ |
| 12 | + typeof(ResourcePostRequestDocument<>), |
| 13 | + typeof(ResourcePatchRequestDocument<>), |
| 14 | + typeof(ToOneRelationshipInRequest<>), |
| 15 | + typeof(NullableToOneRelationshipInRequest<>), |
| 16 | + typeof(ToManyRelationshipInRequest<>) |
| 17 | + ]; |
| 18 | + |
| 19 | + private static readonly Type[] ResponseSchemaTypes = |
| 20 | + [ |
| 21 | + typeof(ResourceCollectionResponseDocument<>), |
| 22 | + typeof(PrimaryResourceResponseDocument<>), |
| 23 | + typeof(SecondaryResourceResponseDocument<>), |
| 24 | + typeof(NullableSecondaryResourceResponseDocument<>), |
| 25 | + typeof(ResourceIdentifierCollectionResponseDocument<>), |
| 26 | + typeof(ResourceIdentifierResponseDocument<>), |
| 27 | + typeof(NullableResourceIdentifierResponseDocument<>), |
| 28 | + typeof(ErrorResponseDocument) |
| 29 | + ]; |
| 30 | + |
| 31 | + private static readonly Type[] SchemaTypesHavingNullableDataProperty = |
| 32 | + [ |
| 33 | + typeof(NullableSecondaryResourceResponseDocument<>), |
| 34 | + typeof(NullableResourceIdentifierResponseDocument<>), |
| 35 | + typeof(NullableToOneRelationshipInRequest<>), |
| 36 | + typeof(NullableToOneRelationshipInResponse<>) |
| 37 | + ]; |
| 38 | + |
| 39 | + private static readonly Type[] RelationshipInResponseSchemaTypes = |
| 40 | + [ |
| 41 | + typeof(ToOneRelationshipInResponse<>), |
| 42 | + typeof(ToManyRelationshipInResponse<>), |
| 43 | + typeof(NullableToOneRelationshipInResponse<>) |
| 44 | + ]; |
| 45 | + |
| 46 | + public static bool RequiresCustomSchemaGenerator(Type schemaType) |
| 47 | + { |
| 48 | + // Subset of the entry types Swashbuckle calls us for, which must be handled by our custom schema generators. |
| 49 | + |
| 50 | + Type lookupType = schemaType.ConstructedToOpenType(); |
| 51 | + return RequestSchemaTypes.Contains(lookupType) || ResponseSchemaTypes.Contains(lookupType); |
| 52 | + } |
| 53 | + |
| 54 | + public static bool IsRequestSchemaType(Type schemaType) |
| 55 | + { |
| 56 | + Type lookupType = schemaType.ConstructedToOpenType(); |
| 57 | + return RequestSchemaTypes.Contains(lookupType); |
| 58 | + } |
| 59 | + |
| 60 | + public static bool HasNullableDataProperty(Type schemaType) |
| 61 | + { |
| 62 | + // Swashbuckle infers non-nullable because our Data properties are [Required]. |
| 63 | + |
| 64 | + Type lookupType = schemaType.ConstructedToOpenType(); |
| 65 | + return SchemaTypesHavingNullableDataProperty.Contains(lookupType); |
| 66 | + } |
| 67 | + |
| 68 | + public static bool IsRelationshipInResponseType(Type schemaType) |
| 69 | + { |
| 70 | + Type lookupType = schemaType.ConstructedToOpenType(); |
| 71 | + return RelationshipInResponseSchemaTypes.Contains(lookupType); |
| 72 | + } |
| 73 | +} |
0 commit comments