Skip to content

Commit 59b5739

Browse files
WretchedDadecaptainsafia
authored andcommitted
Only generate specified document (#59097) (#59535)
1 parent 8eb4970 commit 59b5739

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Tools/GetDocumentInsider/src/Commands/GetDocumentCommandWorker.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,23 @@ private bool GetDocuments(IServiceProvider services)
254254
return false;
255255
}
256256

257-
// If an explicit document name is provided, then generate only that document.
257+
// Get document names
258258
var documentNames = (IEnumerable<string>)InvokeMethod(getDocumentsMethod, service, _getDocumentsArguments);
259259
if (documentNames == null)
260260
{
261261
return false;
262262
}
263263

264-
if (!string.IsNullOrEmpty(_context.DocumentName) && !documentNames.Contains(_context.DocumentName))
264+
// If an explicit document name is provided, then generate only that document.
265+
if (!string.IsNullOrEmpty(_context.DocumentName))
265266
{
266-
_reporter.WriteError(Resources.FormatDocumentNotFound(_context.DocumentName));
267-
return false;
267+
if (!documentNames.Contains(_context.DocumentName))
268+
{
269+
_reporter.WriteError(Resources.FormatDocumentNotFound(_context.DocumentName));
270+
return false;
271+
}
272+
273+
documentNames = [_context.DocumentName];
268274
}
269275

270276
if (!string.IsNullOrWhiteSpace(_context.FileName) && !Regex.IsMatch(_context.FileName, "^([A-Za-z0-9-_]+)$"))

src/Tools/GetDocumentInsider/tests/GetDocumentTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ public void GetDocument_WithDocumentName_Works()
113113
], new GetDocumentCommand(_console), throwOnUnexpectedArg: false);
114114

115115
// Assert
116-
var document = new OpenApiStreamReader().Read(File.OpenRead(Path.Combine(outputPath.FullName, "Sample_internal.json")), out var diagnostic);
116+
var expectedDocumentPath = Path.Combine(outputPath.FullName, "Sample_internal.json");
117+
118+
// There should only be one document when document name is specified
119+
var documentNames = Directory.GetFiles(outputPath.FullName).Where(documentName => documentName.EndsWith(".json", StringComparison.Ordinal)).ToList();
120+
Assert.Single(documentNames);
121+
Assert.Contains(expectedDocumentPath, documentNames);
122+
123+
var document = new OpenApiStreamReader().Read(File.OpenRead(Path.Combine(outputPath.FullName, expectedDocumentPath)), out var diagnostic);
117124
Assert.Empty(diagnostic.Errors);
118125
Assert.Equal(OpenApiSpecVersion.OpenApi3_0, diagnostic.SpecificationVersion);
119126
// Document name in the title gives us a clue that the correct document was actually resolved

0 commit comments

Comments
 (0)