Skip to content

Commit 061f426

Browse files
committed
Changes and tests for support of nullable and required for relationships
1 parent 932367e commit 061f426

File tree

24 files changed

+5885
-13
lines changed

24 files changed

+5885
-13
lines changed

src/JsonApiDotNetCore.OpenApi.Client/JsonApiClient.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ private static void AssertObjectMatchesSerializationScope([SysNotNull] Attribute
185185

186186
private static void SerializeAttributesObject(AttributesObjectInfo alwaysIncludedAttributes, JsonWriter writer, object value, JsonSerializer serializer)
187187
{
188-
AssertRequiredPropertiesAreNotExcluded(value, alwaysIncludedAttributes);
188+
AssertRequiredPropertiesAreNotExcluded(value, alwaysIncludedAttributes, writer);
189189

190190
serializer.ContractResolver = new JsonApiAttributeContractResolver(alwaysIncludedAttributes);
191191
serializer.Serialize(writer, value);
192192
}
193193

194-
private static void AssertRequiredPropertiesAreNotExcluded(object value, AttributesObjectInfo alwaysIncludedAttributes)
194+
private static void AssertRequiredPropertiesAreNotExcluded(object value, AttributesObjectInfo alwaysIncludedAttributes, JsonWriter jsonWriter)
195195
{
196196
PropertyInfo[] propertyInfos = value.GetType().GetProperties();
197197

@@ -204,11 +204,11 @@ private static void AssertRequiredPropertiesAreNotExcluded(object value, Attribu
204204
return;
205205
}
206206

207-
AssertRequiredPropertyIsNotIgnored(value, attributesPropertyInfo);
207+
AssertRequiredPropertyIsNotIgnored(value, attributesPropertyInfo, jsonWriter.Path);
208208
}
209209
}
210210

211-
private static void AssertRequiredPropertyIsNotIgnored(object value, PropertyInfo attribute)
211+
private static void AssertRequiredPropertyIsNotIgnored(object value, PropertyInfo attribute, string path)
212212
{
213213
JsonPropertyAttribute jsonPropertyForAttribute = attribute.GetCustomAttributes<JsonPropertyAttribute>().Single();
214214

@@ -222,7 +222,7 @@ private static void AssertRequiredPropertyIsNotIgnored(object value, PropertyInf
222222
if (isPropertyIgnored)
223223
{
224224
throw new JsonSerializationException(
225-
$"Ignored property '{jsonPropertyForAttribute.PropertyName}' must have a value because it is required. Path 'data.attributes'.");
225+
$"Ignored property '{jsonPropertyForAttribute.PropertyName}' must have a value because it is required. Path '{path}'.");
226226
}
227227
}
228228

@@ -345,3 +345,4 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
345345
}
346346
}
347347
}
348+

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,28 @@ private void ExposeSchema(OpenApiReference openApiReference, Type typeRepresente
112112

113113
private bool IsFieldRequired(ResourceFieldAttribute field)
114114
{
115-
if (field is HasManyAttribute || _resourceTypeInfo.ResourceObjectOpenType != typeof(ResourceObjectInPostRequest<>))
115+
if (_resourceTypeInfo.ResourceObjectOpenType != typeof(ResourceObjectInPostRequest<>))
116116
{
117117
return false;
118118
}
119119

120-
bool hasRequiredAttribute = field.Property.HasAttribute<RequiredAttribute>();
120+
if (field.Property.HasAttribute<RequiredAttribute>())
121+
{
122+
return true;
123+
}
124+
125+
if (field is HasManyAttribute)
126+
{
127+
return false;
128+
}
121129

122130
NullabilityInfoContext nullabilityContext = new();
123131
NullabilityInfo nullabilityInfo = nullabilityContext.Create(field.Property);
124132

125133
return field.Property.PropertyType.IsValueType switch
126134
{
127-
true => hasRequiredAttribute,
128-
false => _options.ValidateModelState ? nullabilityInfo.ReadState == NullabilityState.NotNull || hasRequiredAttribute : hasRequiredAttribute
135+
true => false,
136+
false => _options.ValidateModelState && nullabilityInfo.ReadState == NullabilityState.NotNull
129137
};
130138
}
131139

@@ -230,3 +238,4 @@ private static bool IsDataPropertyNullableInRelationshipSchemaType(Type relation
230238
return NullableRelationshipSchemaOpenTypes.Contains(relationshipSchemaOpenType);
231239
}
232240
}
241+

test/OpenApiClientTests/OpenApiClientTests.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,19 @@
6666
<CodeGenerator>NSwagCSharp</CodeGenerator>
6767
<Options>/UseBaseUrl:false /ClientClassAccessModifier:internal /GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions /GenerateNullableReferenceTypes:false</Options>
6868
</OpenApiReference>
69+
<OpenApiReference Include="SchemaProperties\NullableReferenceTypesEnabled\RelationshipsObject\swagger.g.json">
70+
<Namespace>OpenApiClientTests.SchemaProperties.NullableReferenceTypesEnabled.RelationshipsObject.GeneratedCode</Namespace>
71+
<ClassName>NullableReferenceTypesEnabledClientRelationshipsObject</ClassName>
72+
<OutputPath>NullableReferenceTypesEnabledClientRelationshipsObject.cs</OutputPath>
73+
<CodeGenerator>NSwagCSharp</CodeGenerator>
74+
<Options>/UseBaseUrl:false /ClientClassAccessModifier:internal /GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions /GenerateNullableReferenceTypes:true</Options>
75+
</OpenApiReference>
76+
<OpenApiReference Include="SchemaProperties\NullableReferenceTypesDisabled\RelationshipsObject\swagger.g.json">
77+
<Namespace>OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.RelationshipsObject.GeneratedCode</Namespace>
78+
<ClassName>NullableReferenceTypesDisabledClientRelationshipsObject</ClassName>
79+
<OutputPath>NullableReferenceTypesDisabledClientRelationshipsObject.cs</OutputPath>
80+
<CodeGenerator>NSwagCSharp</CodeGenerator>
81+
<Options>/UseBaseUrl:false /ClientClassAccessModifier:internal /GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions /GenerateNullableReferenceTypes:false</Options>
82+
</OpenApiReference>
6983
</ItemGroup>
7084
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using JsonApiDotNetCore.OpenApi.Client;
2+
using Newtonsoft.Json;
3+
4+
namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.RelationshipsObject.GeneratedCode;
5+
6+
internal partial class NullableReferenceTypesDisabledClientRelationshipsObject : JsonApiClient
7+
{
8+
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
9+
{
10+
SetSerializerSettingsForJsonApi(settings);
11+
12+
settings.Formatting = Formatting.Indented;
13+
}
14+
}

0 commit comments

Comments
 (0)