Skip to content

Support NRT and MSV in required and nullable #1185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 76 commits into from
Oct 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
99dcfa8
Added tests for nullable and required properties in schema generation
maurei Sep 5, 2022
2604f84
Added handling of modelstate validation in setting required attributes
maurei Jul 5, 2022
f7194d9
Not all swagger documents need to be saved to disk; changes in OpenAp…
maurei Sep 5, 2022
449a079
Added OpenApi client tests for nullable and required properties
maurei Sep 6, 2022
c55a2cb
Use NullabilityInfoContext for nullability information
maurei Sep 6, 2022
31638b8
Merge branch 'merge-master-v503-into-openapi' into openapi-required-a…
bkoelman Sep 8, 2022
fc15912
Post-merge fixes
bkoelman Sep 8, 2022
7288b8a
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman Dec 15, 2022
d7c8b4b
Post-merge fixes
bkoelman Dec 15, 2022
d72e43e
Fixed: do not share NullabilityInfoContext, it is not thread-safe
bkoelman Dec 15, 2022
526d03f
Merge pull request #1229 from json-api-dotnet/merge-openapi-into-1185
bkoelman Dec 15, 2022
f8e640b
Review feedback
maurei Dec 19, 2022
7266bc6
remove redundant new lines in eof added by cleanupcode
maurei Dec 19, 2022
945a803
Improved naming in OpenApiTests/SchemaProperties
maurei Dec 19, 2022
f5bb7fc
Review feedback: NullabilityTests
maurei Dec 20, 2022
919a3f8
Improved JsonApiClient and testing
maurei Dec 20, 2022
87aea3e
Fix test: should not omit required field in test request body
maurei Dec 20, 2022
2ff06fc
Temp enable CI buid for current branch
maurei Dec 20, 2022
932367e
Rename test files: it no longer only concerns required attributes, bu…
maurei Dec 20, 2022
061f426
Changes and tests for support of nullable and required for relationships
maurei Dec 22, 2022
90970a8
- Rename placeholder model names and properties to examples consisent…
maurei Dec 24, 2022
affc187
Move into consistent folder structure, remove bad cleanupcode eof lin…
maurei Dec 24, 2022
daf25a2
Merge branch 'openapi' into openapi-required-and-nullable-properties
maurei Dec 24, 2022
d4f1d69
Merge pull request #1231 from json-api-dotnet/attributes-object-schem…
maurei Dec 27, 2022
76e098a
Organise tests such that they map directly to the tables in #1231 and…
maurei Dec 28, 2022
a2e7a96
Add two missing 'Act' comments
maurei Dec 28, 2022
1815dec
More elaborate testing
maurei Jan 3, 2023
63ba43d
Remove non-sensical testcases. Add caching in ObjectExtensions.
maurei Jan 4, 2023
3e9708a
Remove overlooked code duplication in OpenApiTests, revert reflection…
maurei Jan 5, 2023
8304c05
Make AutoFakers deterministic; generate positive IDs
bkoelman Jan 7, 2023
9672b14
Fix nameof
bkoelman Jan 7, 2023
345a689
Use On/Off naming, shorten type names by using Nrt+Msv
bkoelman Jan 7, 2023
22ad300
Renamed EmptyResource to Empty to further shorten FK names
bkoelman Jan 7, 2023
3069533
Fixed invalid EF Core mappings, resulting in logged warnings and inab…
bkoelman Jan 7, 2023
36ac8e7
Move misplaced Act comments
bkoelman Jan 7, 2023
a88bce8
Optimize and clarify ResourceFieldValidationMetadataProvider
bkoelman Jan 8, 2023
2652063
Rename method, provide error message
bkoelman Jan 8, 2023
f2ecb5f
Refactor JsonApiClient: simplified recursion by using two converters,…
bkoelman Jan 8, 2023
d337230
Add relationship nullability assertions in OpenAPI client tests
bkoelman Jan 8, 2023
52cc1f1
Cleanup JsonElementExtensions
bkoelman Jan 8, 2023
b30a680
Cleanup ObjectExtensions
bkoelman Jan 8, 2023
73c3232
Make base type abstract, remove redundant TranslateAsync calls, inlin…
bkoelman Jan 8, 2023
55e702c
Simplify usings
bkoelman Jan 9, 2023
28bb39b
Sync up test names
bkoelman Jan 9, 2023
ff4262e
Fix invalid tests
bkoelman Jan 9, 2023
821d09d
Fix assertion messages
bkoelman Jan 9, 2023
5b71d90
Sync up tests
bkoelman Jan 9, 2023
df9a1b4
Revert change to pass full options instead of just the naming policy
bkoelman Jan 9, 2023
9844e6a
Fix casing in test names
bkoelman Jan 10, 2023
6071f5b
Simplify Cannot_exclude_Id tests
bkoelman Jan 10, 2023
6881208
Rename base type to avoid OpenApiClientTests.OpenApiClientTests
bkoelman Jan 10, 2023
cc3815c
Adapt to existing naming convention
bkoelman Sep 30, 2023
4ef8214
Remove redundant assertions, fix formatting
bkoelman Sep 30, 2023
dcdf5a5
Correct test names
bkoelman Sep 30, 2023
2d7f8b9
Centralize code for property assignment in tests
bkoelman Oct 1, 2023
7364e94
Merge pull request #1244 from json-api-dotnet/openapi-nrt-msv-updates
bkoelman Oct 1, 2023
7c6f9eb
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman Oct 1, 2023
41d6d67
Apply Resharper hint: convert switch statement to expression
bkoelman Oct 1, 2023
bd31216
Simplify expressions
bkoelman Oct 1, 2023
6a87751
Simplify exception assertions
bkoelman Oct 1, 2023
5c85c3a
Use string interpolation
bkoelman Oct 1, 2023
b08ab9b
Corrections in openapi documentation
bkoelman Oct 1, 2023
f7786bb
Simplify code
bkoelman Oct 1, 2023
c3c4844
Remove redundant suppression
bkoelman Oct 1, 2023
66a2dc4
Merge branch 'master' into openapi-required-and-nullable-properties
bkoelman Oct 1, 2023
f46db8a
Combine OpenAPI client tests for create resource with null/default at…
bkoelman Oct 1, 2023
63bf071
Fixup OpenAPI example and docs
bkoelman Oct 1, 2023
ce8045d
Revert "Merge branch 'master' into openapi-required-and-nullable-prop…
bkoelman Oct 1, 2023
fac0471
Workaround for running OpenAPI tests on Windows
bkoelman Oct 2, 2023
dd667be
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman Oct 2, 2023
9194487
Address failing InspectCode
bkoelman Oct 2, 2023
85d608b
Remove redundant calls
bkoelman Oct 2, 2023
007ff30
Remove redundant tests
bkoelman Oct 2, 2023
817aeb3
Move types out of the wrong namespace
bkoelman Oct 2, 2023
fb9b12a
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman Oct 4, 2023
0dd49b0
Remove redundant suppressions in openapi after update to CSharpGuidel…
bkoelman Oct 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optimize and clarify ResourceFieldValidationMetadataProvider
  • Loading branch information
bkoelman committed Jan 8, 2023
commit a88bce8474e0358621a4add94107d28bf6fd5474
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace JsonApiDotNetCore.OpenApi;
internal sealed class ResourceFieldValidationMetadataProvider
{
private readonly bool _validateModelState;
private readonly NullabilityInfoContext _nullabilityContext;
private readonly NullabilityInfoContext _nullabilityContext = new();
private readonly IModelMetadataProvider _modelMetadataProvider;

public ResourceFieldValidationMetadataProvider(IJsonApiOptions options, IModelMetadataProvider modelMetadataProvider)
Expand All @@ -20,7 +20,6 @@ public ResourceFieldValidationMetadataProvider(IJsonApiOptions options, IModelMe

_validateModelState = options.ValidateModelState;
_modelMetadataProvider = modelMetadataProvider;
_nullabilityContext = new NullabilityInfoContext();
}

public bool IsNullable(ResourceFieldAttribute field)
Expand All @@ -33,18 +32,13 @@ public bool IsNullable(ResourceFieldAttribute field)
}

bool hasRequiredAttribute = field.Property.HasAttribute<RequiredAttribute>();
NullabilityInfo nullabilityInfo = _nullabilityContext.Create(field.Property);

if (field is HasManyAttribute)
{
return false;
}

if (hasRequiredAttribute && _validateModelState && nullabilityInfo.ReadState != NullabilityState.NotNull)
if (_validateModelState && hasRequiredAttribute)
{
return false;
}

NullabilityInfo nullabilityInfo = _nullabilityContext.Create(field.Property);
return nullabilityInfo.ReadState != NullabilityState.NotNull;
}

Expand All @@ -64,11 +58,12 @@ public bool IsRequired(ResourceFieldAttribute field)
return false;
}

bool isNotNull = HasNullabilityStateNotNull(field);
bool isRequiredValueType = field.Property.PropertyType.IsValueType && hasRequiredAttribute && isNotNull;
NullabilityInfo nullabilityInfo = _nullabilityContext.Create(field.Property);
bool isRequiredValueType = field.Property.PropertyType.IsValueType && hasRequiredAttribute && nullabilityInfo.ReadState == NullabilityState.NotNull;

if (isRequiredValueType)
{
// Special case: ASP.NET ModelState Validation effectively ignores value types with [Required].
return false;
}

Expand All @@ -77,16 +72,9 @@ public bool IsRequired(ResourceFieldAttribute field)

private bool IsModelStateValidationRequired(ResourceFieldAttribute field)
{
ModelMetadata resourceFieldModelMetadata = _modelMetadataProvider.GetMetadataForProperties(field.Type.ClrType)
.Single(modelMetadata => modelMetadata.PropertyName! == field.Property.Name);
ModelMetadata modelMetadata = _modelMetadataProvider.GetMetadataForProperty(field.Type.ClrType, field.Property.Name);

return resourceFieldModelMetadata.ValidatorMetadata.Any(validatorMetadata => validatorMetadata is RequiredAttribute);
}

private bool HasNullabilityStateNotNull(ResourceFieldAttribute field)
{
NullabilityInfo resourceFieldNullabilityInfo = _nullabilityContext.Create(field.Property);
bool hasNullabilityStateNotNull = resourceFieldNullabilityInfo is { ReadState: NullabilityState.NotNull, WriteState: NullabilityState.NotNull };
return hasNullabilityStateNotNull;
// Non-nullable reference types are implicitly required, unless SuppressImplicitRequiredAttributeForNonNullableReferenceTypes is set.
return modelMetadata.ValidatorMetadata.Any(validatorMetadata => validatorMetadata is RequiredAttribute);
}
}