-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Changes from 5 commits
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 2604f84
Added handling of modelstate validation in setting required attributes
maurei f7194d9
Not all swagger documents need to be saved to disk; changes in OpenAp…
maurei 449a079
Added OpenApi client tests for nullable and required properties
maurei c55a2cb
Use NullabilityInfoContext for nullability information
maurei 31638b8
Merge branch 'merge-master-v503-into-openapi' into openapi-required-a…
bkoelman fc15912
Post-merge fixes
bkoelman 7288b8a
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman d7c8b4b
Post-merge fixes
bkoelman d72e43e
Fixed: do not share NullabilityInfoContext, it is not thread-safe
bkoelman 526d03f
Merge pull request #1229 from json-api-dotnet/merge-openapi-into-1185
bkoelman f8e640b
Review feedback
maurei 7266bc6
remove redundant new lines in eof added by cleanupcode
maurei 945a803
Improved naming in OpenApiTests/SchemaProperties
maurei f5bb7fc
Review feedback: NullabilityTests
maurei 919a3f8
Improved JsonApiClient and testing
maurei 87aea3e
Fix test: should not omit required field in test request body
maurei 2ff06fc
Temp enable CI buid for current branch
maurei 932367e
Rename test files: it no longer only concerns required attributes, bu…
maurei 061f426
Changes and tests for support of nullable and required for relationships
maurei 90970a8
- Rename placeholder model names and properties to examples consisent…
maurei affc187
Move into consistent folder structure, remove bad cleanupcode eof lin…
maurei daf25a2
Merge branch 'openapi' into openapi-required-and-nullable-properties
maurei d4f1d69
Merge pull request #1231 from json-api-dotnet/attributes-object-schem…
maurei 76e098a
Organise tests such that they map directly to the tables in #1231 and…
maurei a2e7a96
Add two missing 'Act' comments
maurei 1815dec
More elaborate testing
maurei 63ba43d
Remove non-sensical testcases. Add caching in ObjectExtensions.
maurei 3e9708a
Remove overlooked code duplication in OpenApiTests, revert reflection…
maurei 8304c05
Make AutoFakers deterministic; generate positive IDs
bkoelman 9672b14
Fix nameof
bkoelman 345a689
Use On/Off naming, shorten type names by using Nrt+Msv
bkoelman 22ad300
Renamed EmptyResource to Empty to further shorten FK names
bkoelman 3069533
Fixed invalid EF Core mappings, resulting in logged warnings and inab…
bkoelman 36ac8e7
Move misplaced Act comments
bkoelman a88bce8
Optimize and clarify ResourceFieldValidationMetadataProvider
bkoelman 2652063
Rename method, provide error message
bkoelman f2ecb5f
Refactor JsonApiClient: simplified recursion by using two converters,…
bkoelman d337230
Add relationship nullability assertions in OpenAPI client tests
bkoelman 52cc1f1
Cleanup JsonElementExtensions
bkoelman b30a680
Cleanup ObjectExtensions
bkoelman 73c3232
Make base type abstract, remove redundant TranslateAsync calls, inlin…
bkoelman 55e702c
Simplify usings
bkoelman 28bb39b
Sync up test names
bkoelman ff4262e
Fix invalid tests
bkoelman 821d09d
Fix assertion messages
bkoelman 5b71d90
Sync up tests
bkoelman df9a1b4
Revert change to pass full options instead of just the naming policy
bkoelman 9844e6a
Fix casing in test names
bkoelman 6071f5b
Simplify Cannot_exclude_Id tests
bkoelman 6881208
Rename base type to avoid OpenApiClientTests.OpenApiClientTests
bkoelman cc3815c
Adapt to existing naming convention
bkoelman 4ef8214
Remove redundant assertions, fix formatting
bkoelman dcdf5a5
Correct test names
bkoelman 2d7f8b9
Centralize code for property assignment in tests
bkoelman 7364e94
Merge pull request #1244 from json-api-dotnet/openapi-nrt-msv-updates
bkoelman 7c6f9eb
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman 41d6d67
Apply Resharper hint: convert switch statement to expression
bkoelman bd31216
Simplify expressions
bkoelman 6a87751
Simplify exception assertions
bkoelman 5c85c3a
Use string interpolation
bkoelman b08ab9b
Corrections in openapi documentation
bkoelman f7786bb
Simplify code
bkoelman c3c4844
Remove redundant suppression
bkoelman 66a2dc4
Merge branch 'master' into openapi-required-and-nullable-properties
bkoelman f46db8a
Combine OpenAPI client tests for create resource with null/default at…
bkoelman 63bf071
Fixup OpenAPI example and docs
bkoelman ce8045d
Revert "Merge branch 'master' into openapi-required-and-nullable-prop…
bkoelman fac0471
Workaround for running OpenAPI tests on Windows
bkoelman dd667be
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman 9194487
Address failing InspectCode
bkoelman 85d608b
Remove redundant calls
bkoelman 007ff30
Remove redundant tests
bkoelman 817aeb3
Move types out of the wrong namespace
bkoelman fb9b12a
Merge branch 'openapi' into openapi-required-and-nullable-properties
bkoelman 0dd49b0
Remove redundant suppressions in openapi after update to CSharpGuidel…
bkoelman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
31 changes: 25 additions & 6 deletions
31
src/JsonApiDotNetCore.OpenApi/ResourceFieldAttributeExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Reflection; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace JsonApiDotNetCore.OpenApi; | ||
|
||
internal static class ResourceFieldAttributeExtensions | ||
{ | ||
private static readonly NullabilityInfoContext NullabilityInfoContext = new(); | ||
|
||
public static bool IsNullable(this ResourceFieldAttribute source) | ||
{ | ||
TypeCategory fieldTypeCategory = source.Property.GetTypeCategory(); | ||
bool hasRequiredAttribute = source.Property.HasAttribute<RequiredAttribute>(); | ||
|
||
return fieldTypeCategory switch | ||
if (hasRequiredAttribute) | ||
{ | ||
TypeCategory.NonNullableReferenceType or TypeCategory.ValueType => false, | ||
TypeCategory.NullableReferenceType or TypeCategory.NullableValueType => !hasRequiredAttribute, | ||
_ => throw new UnreachableCodeException() | ||
}; | ||
// Reflects the following cases, independent of NRT setting | ||
// `[Required] int? Number` => not nullable | ||
// `[Required] int Number` => not nullable | ||
// `[Required] string Text` => not nullable | ||
// `[Required] string? Text` => not nullable | ||
// `[Required] string Text` => not nullable | ||
return false; | ||
} | ||
|
||
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(source.Property); | ||
|
||
// Reflects the following cases: | ||
// Independent of NRT: | ||
// `int? Number` => nullable | ||
// `int Number` => not nullable | ||
// If NRT is enabled: | ||
// `string? Text` => nullable | ||
// `string Text` => not nullable | ||
// If NRT is disabled: | ||
// `string Text` => nullable | ||
return nullabilityInfo.ReadState is not NullabilityState.NotNull; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/OpenApiClientTests/PropertyInfoAssertionsExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using FluentAssertions.Types; | ||
|
||
namespace OpenApiClientTests; | ||
|
||
internal static class PropertyInfoAssertionsExtensions | ||
{ | ||
private static readonly NullabilityInfoContext NullabilityInfoContext = new(); | ||
|
||
[CustomAssertion] | ||
public static void BeNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs) | ||
{ | ||
PropertyInfo propertyInfo = source.Subject; | ||
|
||
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(propertyInfo); | ||
|
||
nullabilityInfo.ReadState.Should().NotBe(NullabilityState.NotNull, because, becauseArgs); | ||
} | ||
|
||
[CustomAssertion] | ||
public static void BeNonNullable(this PropertyInfoAssertions source, string because = "", params object[] becauseArgs) | ||
{ | ||
PropertyInfo propertyInfo = source.Subject; | ||
|
||
NullabilityInfo nullabilityInfo = NullabilityInfoContext.Create(propertyInfo); | ||
|
||
nullabilityInfo.ReadState.Should().Be(NullabilityState.NotNull, because, becauseArgs); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ties/NullableReferenceTypesDisabled/GeneratedCode/NullableReferenceTypesDisabledClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using JsonApiDotNetCore.OpenApi.Client; | ||
using Newtonsoft.Json; | ||
|
||
namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.GeneratedCode; | ||
|
||
internal partial class NullableReferenceTypesDisabledClient : JsonApiClient | ||
{ | ||
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings) | ||
{ | ||
SetSerializerSettingsForJsonApi(settings); | ||
|
||
settings.Formatting = Formatting.Indented; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
test/OpenApiClientTests/SchemaProperties/NullableReferenceTypesDisabled/NullabilityTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.GeneratedCode; | ||
using Xunit; | ||
|
||
namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled; | ||
|
||
public sealed class NullabilityTests | ||
{ | ||
[Fact] | ||
public void Nullability_of_generated_types_is_as_expected() | ||
{ | ||
PropertyInfo[] propertyInfos = typeof(ChickenAttributesInResponse).GetProperties(); | ||
|
||
PropertyInfo? propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Name)); | ||
propertyInfo.Should().BeNullable(); | ||
|
||
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.NameOfCurrentFarm)); | ||
propertyInfo.Should().BeNullable(); | ||
|
||
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.Age)); | ||
propertyInfo.Should().BeNonNullable(); | ||
|
||
propertyInfo = propertyInfos.FirstOrDefault(property => property.Name == nameof(ChickenAttributesInResponse.TimeAtCurrentFarmInDays)); | ||
propertyInfo.Should().BeNullable(); | ||
} | ||
bkoelman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.