Skip to content

Commit 6365b13

Browse files
committed
Fix generated documentation on relationships
1 parent 9d82fcd commit 6365b13

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,14 @@ public void SetMembersOfRelationshipsObject(OpenApiSchema fullSchemaForRelations
154154
{
155155
ArgumentGuard.NotNull(fullSchemaForRelationshipsObject);
156156

157-
foreach ((string fieldName, OpenApiSchema resourceFieldSchema) in _schemasForResourceFields)
157+
foreach (string fieldName in _schemasForResourceFields.Keys)
158158
{
159159
RelationshipAttribute? matchingRelationship = _resourceTypeInfo.ResourceType.FindRelationshipByPublicName(fieldName);
160160

161161
if (matchingRelationship != null)
162162
{
163163
EnsureResourceIdentifierObjectSchemaExists(matchingRelationship);
164164
AddRelationshipSchemaToResourceObject(matchingRelationship, fullSchemaForRelationshipsObject);
165-
166-
// This currently has no effect because $ref cannot be combined with other elements in OAS 3.0.
167-
// This can be worked around by using the allOf operator. See https://github.com/OAI/OpenAPI-Specification/issues/1514.
168-
resourceFieldSchema.Description = _resourceObjectDocumentationReader.GetDocumentationForRelationship(matchingRelationship);
169165
}
170166
}
171167
}
@@ -209,7 +205,8 @@ private void AddRelationshipSchemaToResourceObject(RelationshipAttribute relatio
209205
AllOf = new List<OpenApiSchema>
210206
{
211207
referenceSchemaForRelationship
212-
}
208+
},
209+
Description = _resourceObjectDocumentationReader.GetDocumentationForRelationship(relationship)
213210
};
214211

215212
fullSchemaForRelationshipsObject.Properties.Add(relationship.PublicName, extendedReferenceSchemaForRelationship);

test/OpenApiTests/DocComments/DocCommentsTests.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public async Task Resource_types_are_documented()
449449
}
450450

451451
[Fact]
452-
public async Task Resource_attributes_are_documented()
452+
public async Task Attributes_are_documented()
453453
{
454454
// Act
455455
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
@@ -471,6 +471,43 @@ public async Task Resource_attributes_are_documented()
471471
});
472472
}
473473

474+
[Fact]
475+
public async Task Relationships_are_documented()
476+
{
477+
// Act
478+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
479+
480+
// Assert
481+
document.Should().ContainPath("components.schemas").With(schemasElement =>
482+
{
483+
schemasElement.Should().HaveProperty("elevatorRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
484+
schemasElement.Should().HaveProperty("elevatorRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
485+
schemasElement.Should().HaveProperty("elevatorRelationshipsInResponse.properties.existsIn.description", "The skyscraper this elevator exists in.");
486+
487+
schemasElement.Should().ContainPath("skyscraperRelationshipsInPatchRequest.properties").With(propertiesElement =>
488+
{
489+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
490+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
491+
});
492+
493+
schemasElement.Should().ContainPath("skyscraperRelationshipsInPostRequest.properties").With(propertiesElement =>
494+
{
495+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
496+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
497+
});
498+
499+
schemasElement.Should().ContainPath("skyscraperRelationshipsInResponse.properties").With(propertiesElement =>
500+
{
501+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
502+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
503+
});
504+
505+
schemasElement.Should().HaveProperty("spaceRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this space exists in.");
506+
schemasElement.Should().HaveProperty("spaceRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this space exists in.");
507+
schemasElement.Should().HaveProperty("spaceRelationshipsInResponse.properties.existsIn.description", "The skyscraper this space exists in.");
508+
});
509+
}
510+
474511
[Fact]
475512
public async Task Enums_are_documented()
476513
{

0 commit comments

Comments
 (0)