Skip to content

Commit

Permalink
Fix generated documentation on relationships and enum properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bkoelman committed Oct 27, 2023
1 parent 05e4506 commit 072ed6d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,14 @@ public void SetMembersOfRelationshipsObject(OpenApiSchema fullSchemaForRelations
{
ArgumentGuard.NotNull(fullSchemaForRelationshipsObject);

foreach ((string fieldName, OpenApiSchema resourceFieldSchema) in _schemasForResourceFields)
foreach (string fieldName in _schemasForResourceFields.Keys)
{
RelationshipAttribute? matchingRelationship = _resourceTypeInfo.ResourceType.FindRelationshipByPublicName(fieldName);

if (matchingRelationship != null)
{
EnsureResourceIdentifierObjectSchemaExists(matchingRelationship);
AddRelationshipSchemaToResourceObject(matchingRelationship, fullSchemaForRelationshipsObject);

// This currently has no effect because $ref cannot be combined with other elements in OAS 3.0.
// This can be worked around by using the allOf operator. See https://github.com/OAI/OpenAPI-Specification/issues/1514.
resourceFieldSchema.Description = _resourceObjectDocumentationReader.GetDocumentationForRelationship(matchingRelationship);
}
}
}
Expand Down Expand Up @@ -209,7 +205,8 @@ private void AddRelationshipSchemaToResourceObject(RelationshipAttribute relatio
AllOf = new List<OpenApiSchema>
{
referenceSchemaForRelationship
}
},
Description = _resourceObjectDocumentationReader.GetDocumentationForRelationship(relationship)
};

fullSchemaForRelationshipsObject.Properties.Add(relationship.PublicName, extendedReferenceSchemaForRelationship);
Expand Down
59 changes: 55 additions & 4 deletions test/OpenApiTests/DocComments/DocCommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public async Task Resource_types_are_documented()
}

[Fact]
public async Task Resource_attributes_are_documented()
public async Task Attributes_are_documented()
{
// Act
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
Expand All @@ -465,9 +465,60 @@ public async Task Resource_attributes_are_documented()
schemasElement.Should().HaveProperty("skyscraperAttributesInPostRequest.properties.heightInMeters.description", "The height of this building, in meters.");
schemasElement.Should().HaveProperty("skyscraperAttributesInResponse.properties.heightInMeters.description", "The height of this building, in meters.");
schemasElement.Should().HaveProperty("spaceAttributesInPatchRequest.properties.floorNumber.description", "The floor number on which this space resides.");
schemasElement.Should().HaveProperty("spaceAttributesInPostRequest.properties.floorNumber.description", "The floor number on which this space resides.");
schemasElement.Should().HaveProperty("spaceAttributesInResponse.properties.floorNumber.description", "The floor number on which this space resides.");
schemasElement.Should().ContainPath("spaceAttributesInPatchRequest.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
});
schemasElement.Should().ContainPath("spaceAttributesInPostRequest.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
});
schemasElement.Should().ContainPath("spaceAttributesInResponse.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("floorNumber.description", "The floor number on which this space resides.");
propertiesElement.Should().HaveProperty("kind.description", "The kind of this space.");
});
});
}

[Fact]
public async Task Relationships_are_documented()
{
// Act
JsonElement document = await _testContext.GetSwaggerDocumentAsync();

// Assert
document.Should().ContainPath("components.schemas").With(schemasElement =>
{
schemasElement.Should().HaveProperty("elevatorRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
schemasElement.Should().HaveProperty("elevatorRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this elevator exists in.");
schemasElement.Should().HaveProperty("elevatorRelationshipsInResponse.properties.existsIn.description", "The skyscraper this elevator exists in.");
schemasElement.Should().ContainPath("skyscraperRelationshipsInPatchRequest.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
});
schemasElement.Should().ContainPath("skyscraperRelationshipsInPostRequest.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
});
schemasElement.Should().ContainPath("skyscraperRelationshipsInResponse.properties").With(propertiesElement =>
{
propertiesElement.Should().HaveProperty("elevator.description", "An optional elevator within this building, providing access to spaces.");
propertiesElement.Should().HaveProperty("spaces.description", "The spaces within this building.");
});
schemasElement.Should().HaveProperty("spaceRelationshipsInPatchRequest.properties.existsIn.description", "The skyscraper this space exists in.");
schemasElement.Should().HaveProperty("spaceRelationshipsInPostRequest.properties.existsIn.description", "The skyscraper this space exists in.");
schemasElement.Should().HaveProperty("spaceRelationshipsInResponse.properties.existsIn.description", "The skyscraper this space exists in.");
});
}

Expand Down

0 comments on commit 072ed6d

Please sign in to comment.