Skip to content

Commit 072ed6d

Browse files
committed
Fix generated documentation on relationships and enum properties
1 parent 05e4506 commit 072ed6d

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
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: 55 additions & 4 deletions
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();
@@ -465,9 +465,60 @@ public async Task Resource_attributes_are_documented()
465465
schemasElement.Should().HaveProperty("skyscraperAttributesInPostRequest.properties.heightInMeters.description", "The height of this building, in meters.");
466466
schemasElement.Should().HaveProperty("skyscraperAttributesInResponse.properties.heightInMeters.description", "The height of this building, in meters.");
467467

468-
schemasElement.Should().HaveProperty("spaceAttributesInPatchRequest.properties.floorNumber.description", "The floor number on which this space resides.");
469-
schemasElement.Should().HaveProperty("spaceAttributesInPostRequest.properties.floorNumber.description", "The floor number on which this space resides.");
470-
schemasElement.Should().HaveProperty("spaceAttributesInResponse.properties.floorNumber.description", "The floor number on which this space resides.");
468+
schemasElement.Should().ContainPath("spaceAttributesInPatchRequest.properties").With(propertiesElement =>
469+
{
470+
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
471+
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
472+
});
473+
474+
schemasElement.Should().ContainPath("spaceAttributesInPostRequest.properties").With(propertiesElement =>
475+
{
476+
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
477+
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
478+
});
479+
480+
schemasElement.Should().ContainPath("spaceAttributesInResponse.properties").With(propertiesElement =>
481+
{
482+
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
483+
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
484+
});
485+
});
486+
}
487+
488+
[Fact]
489+
public async Task Relationships_are_documented()
490+
{
491+
// Act
492+
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
493+
494+
// Assert
495+
document.Should().ContainPath("components.schemas").With(schemasElement =>
496+
{
497+
schemasElement.Should().HaveProperty("elevatorRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
498+
schemasElement.Should().HaveProperty("elevatorRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
499+
schemasElement.Should().HaveProperty("elevatorRelationshipsInResponse.properties.existsIn.description", "The skyscraper this elevator exists in.");
500+
501+
schemasElement.Should().ContainPath("skyscraperRelationshipsInPatchRequest.properties").With(propertiesElement =>
502+
{
503+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
504+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
505+
});
506+
507+
schemasElement.Should().ContainPath("skyscraperRelationshipsInPostRequest.properties").With(propertiesElement =>
508+
{
509+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
510+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
511+
});
512+
513+
schemasElement.Should().ContainPath("skyscraperRelationshipsInResponse.properties").With(propertiesElement =>
514+
{
515+
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
516+
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
517+
});
518+
519+
schemasElement.Should().HaveProperty("spaceRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this space exists in.");
520+
schemasElement.Should().HaveProperty("spaceRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this space exists in.");
521+
schemasElement.Should().HaveProperty("spaceRelationshipsInResponse.properties.existsIn.description", "The skyscraper this space exists in.");
471522
});
472523
}
473524

0 commit comments

Comments
 (0)