Skip to content

Commit 8f0405a

Browse files
committed
Correct the nullability of values in "meta" (IDictionary<string, object> becomes IDictionary<string, object?>)
1 parent abc9fff commit 8f0405a

File tree

17 files changed

+2249
-312
lines changed

17 files changed

+2249
-312
lines changed

src/Examples/QueryStringExample/swagger.json

+1,602
Large diffs are not rendered by default.

src/JsonApiDotNetCore.OpenApi/OpenApiSchemaExtensions.cs

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ namespace JsonApiDotNetCore.OpenApi;
44

55
internal static class OpenApiSchemaExtensions
66
{
7+
public static void ReorderProperties(this OpenApiSchema fullSchema, IEnumerable<string> propertyNamesInOrder)
8+
{
9+
ArgumentGuard.NotNull(fullSchema);
10+
ArgumentGuard.NotNull(propertyNamesInOrder);
11+
12+
var propertiesInOrder = new Dictionary<string, OpenApiSchema>();
13+
14+
foreach (string propertyName in propertyNamesInOrder)
15+
{
16+
if (fullSchema.Properties.TryGetValue(propertyName, out OpenApiSchema? schema))
17+
{
18+
propertiesInOrder.Add(propertyName, schema);
19+
}
20+
}
21+
22+
if (fullSchema.Properties.Count != propertiesInOrder.Count)
23+
{
24+
throw new UnreachableCodeException();
25+
}
26+
27+
fullSchema.Properties = propertiesInOrder;
28+
}
29+
730
public static OpenApiSchema UnwrapExtendedReferenceSchema(this OpenApiSchema source)
831
{
932
ArgumentGuard.NotNull(source);
@@ -15,4 +38,18 @@ public static OpenApiSchema UnwrapExtendedReferenceSchema(this OpenApiSchema sou
1538

1639
return source.AllOf.Single();
1740
}
41+
42+
public static void SetValuesInMetaToNullable(this OpenApiSchema fullSchema)
43+
{
44+
ArgumentGuard.NotNull(fullSchema);
45+
46+
if (fullSchema.Properties.TryGetValue(JsonApiPropertyName.Meta, out OpenApiSchema? schemaForMeta))
47+
{
48+
schemaForMeta.AdditionalProperties = new OpenApiSchema
49+
{
50+
Type = "object",
51+
Nullable = true
52+
};
53+
}
54+
}
1855
}

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiSchemaGenerator.cs

+22-12
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ public OpenApiSchema GenerateSchema(Type modelType, SchemaRepository schemaRepos
8383

8484
if (IsJsonApiDocument(modelType))
8585
{
86-
OpenApiSchema schema = GenerateJsonApiDocumentSchema(modelType);
86+
OpenApiSchema referenceSchemaForDocument = GenerateJsonApiDocumentSchema(modelType);
87+
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
8788

8889
if (IsDataPropertyNullableInDocument(modelType))
8990
{
90-
SetDataObjectSchemaToNullable(schema);
91+
SetDataObjectSchemaToNullable(fullSchemaForDocument);
9192
}
9293

93-
if (!_options.IncludeJsonApiVersion)
94-
{
95-
RemoveJsonApiObject(schema);
96-
}
94+
fullSchemaForDocument.SetValuesInMetaToNullable();
95+
96+
SetJsonApiVersion(fullSchemaForDocument);
9797

9898
// Schema might depend on other schemas not handled by us, so should not return here.
9999
}
@@ -150,20 +150,30 @@ private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSc
150150
};
151151
}
152152

153-
private void SetDataObjectSchemaToNullable(OpenApiSchema referenceSchemaForDocument)
153+
private void SetDataObjectSchemaToNullable(OpenApiSchema fullSchemaForDocument)
154154
{
155-
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
156155
OpenApiSchema referenceSchemaForData = fullSchemaForDocument.Properties[JsonApiPropertyName.Data];
157156
referenceSchemaForData.Nullable = true;
158157
fullSchemaForDocument.Properties[JsonApiPropertyName.Data] = referenceSchemaForData;
159158
}
160159

161-
private void RemoveJsonApiObject(OpenApiSchema referenceSchemaForDocument)
160+
private void SetJsonApiVersion(OpenApiSchema fullSchemaForDocument)
162161
{
163-
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
164-
fullSchemaForDocument.Properties.Remove(JsonApiPropertyName.Jsonapi);
162+
if (fullSchemaForDocument.Properties.TryGetValue(JsonApiPropertyName.Jsonapi, out OpenApiSchema? referenceSchemaForJsonapi))
163+
{
164+
string jsonapiSchemaId = referenceSchemaForJsonapi.AllOf[0].Reference.Id;
165165

166-
_schemaRepositoryAccessor.Current.Schemas.Remove("jsonapi-object");
166+
if (!_options.IncludeJsonApiVersion)
167+
{
168+
fullSchemaForDocument.Properties.Remove(JsonApiPropertyName.Jsonapi);
169+
_schemaRepositoryAccessor.Current.Schemas.Remove(jsonapiSchemaId);
170+
}
171+
else
172+
{
173+
OpenApiSchema fullSchemaForJsonapi = _schemaRepositoryAccessor.Current.Schemas[jsonapiSchemaId];
174+
fullSchemaForJsonapi.SetValuesInMetaToNullable();
175+
}
176+
}
167177
}
168178

169179
private static OpenApiSchema CreateExtendedReferenceSchema(OpenApiSchema referenceSchemaForResourceObject)

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/OpenApiSchemaExtensions.cs

-29
This file was deleted.

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ private OpenApiSchema CreateRelationshipReferenceSchema(Type relationshipSchemaT
244244
{
245245
fullSchema.Required.Remove(JsonApiPropertyName.Data);
246246

247+
fullSchema.SetValuesInMetaToNullable();
248+
247249
fullSchema.ReorderProperties(RelationshipObjectPropertyNamesInOrder);
248250
}
249251

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceObjectSchemaGenerator.cs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public OpenApiSchema GenerateSchema(Type resourceObjectType)
5959
RemoveResourceIdIfPostResourceObject(resourceTypeInfo, fullSchemaForResourceObject);
6060

6161
SetResourceType(fullSchemaForResourceObject, resourceTypeInfo.ResourceType);
62+
fullSchemaForResourceObject.SetValuesInMetaToNullable();
6263

6364
SetResourceAttributes(fullSchemaForResourceObject, fieldObjectBuilder);
6465

0 commit comments

Comments
 (0)