Skip to content

Commit 46f6a5b

Browse files
CopilotJerryNixon
andcommitted
Address code review feedback: fix tag key mismatch and improve code quality
Co-authored-by: JerryNixon <[email protected]>
1 parent 9d7ece7 commit 46f6a5b

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/Core/Services/OpenAPI/OpenApiDocumentor.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,16 @@ public void CreateDocument(bool doOverrideExistingDocument = false)
145145
foreach (KeyValuePair<string, Entity> kvp in runtimeConfig.Entities)
146146
{
147147
Entity entity = kvp.Value;
148-
string restPath = entity.Rest?.Path ?? kvp.Key;
148+
// Use GetEntityRestPath to ensure consistent path computation (with leading slash trimmed)
149+
string restPath = GetEntityRestPath(entity.Rest, kvp.Key);
149150

150151
// Only add the tag if it hasn't been added yet (handles entities with the same REST path)
151-
if (!globalTagsDict.ContainsKey(restPath))
152+
// First entity's description wins when multiple entities share the same REST path.
153+
globalTagsDict.TryAdd(restPath, new OpenApiTag
152154
{
153-
globalTagsDict[restPath] = new OpenApiTag
154-
{
155-
Name = restPath,
156-
Description = string.IsNullOrWhiteSpace(entity.Description) ? null : entity.Description
157-
};
158-
}
155+
Name = restPath,
156+
Description = string.IsNullOrWhiteSpace(entity.Description) ? null : entity.Description
157+
});
159158
}
160159

161160
OpenApiDocument doc = new()
@@ -243,7 +242,9 @@ private OpenApiPaths BuildPaths(RuntimeEntities entities, string defaultDataSour
243242
}
244243
else
245244
{
246-
// Fallback: create a new tag if not found in global tags (should not happen in normal flow)
245+
// Fallback: create a new tag if not found in global tags.
246+
// This should not happen in normal flow if GetEntityRestPath is used consistently.
247+
// If this path is reached, it indicates a key mismatch between CreateDocument and BuildPaths.
247248
tags.Add(new OpenApiTag
248249
{
249250
Name = entityRestPath,

src/Service.Tests/OpenApiDocumentor/StoredProcedureGeneration.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,21 @@ public void OpenApiDocumentor_NoDuplicateTags()
161161
IList<OpenApiTag> tags = _openApiDocument.Tags;
162162

163163
// Get all tag names
164-
var tagNames = tags.Select(t => t.Name).ToList();
164+
List<string> tagNames = tags.Select(t => t.Name).ToList();
165165

166166
// Get distinct tag names
167-
var distinctTagNames = tagNames.Distinct().ToList();
167+
List<string> distinctTagNames = tagNames.Distinct().ToList();
168168

169169
// Assert: The number of tags should equal the number of distinct tag names (no duplicates)
170170
Assert.AreEqual(distinctTagNames.Count, tagNames.Count,
171171
$"Duplicate tags found in OpenAPI document. Tags: {string.Join(", ", tagNames)}");
172172

173173
// Additionally, verify that each operation references tags that are in the global tags list
174-
foreach (var path in _openApiDocument.Paths)
174+
foreach (KeyValuePair<string, OpenApiPathItem> path in _openApiDocument.Paths)
175175
{
176-
foreach (var operation in path.Value.Operations)
176+
foreach (KeyValuePair<OperationType, OpenApiOperation> operation in path.Value.Operations)
177177
{
178-
foreach (var operationTag in operation.Value.Tags)
178+
foreach (OpenApiTag operationTag in operation.Value.Tags)
179179
{
180180
// Verify that the operation's tag is the same instance as one in the global tags
181181
bool foundMatchingTag = tags.Any(globalTag => ReferenceEquals(globalTag, operationTag));

0 commit comments

Comments
 (0)