Skip to content

Commit 0c342ad

Browse files
authored
Merge branch 'main' into mw/use-http-method-object-instead-of-enum
2 parents c6e6be2 + fb496b4 commit 0c342ad

File tree

8 files changed

+140
-54
lines changed

8 files changed

+140
-54
lines changed

docs/images/workbench.png

-33 KB
Loading

src/Microsoft.OpenApi.Hidi/readme.md

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ Install [Microsoft.OpenApi.Hidi](https://www.nuget.org/packages/Microsoft.OpenAp
1717

1818
### .NET CLI(Global)
1919

20-
1. dotnet tool install --global Microsoft.OpenApi.Hidi --prerelease
21-
20+
```bash
21+
dotnet tool install --global Microsoft.OpenApi.Hidi --prerelease
22+
```
2223

2324
### .NET CLI(local)
24-
25-
1. dotnet new tool-manifest #if you are setting up the OpenAPI.NET repo
26-
2. dotnet tool install --local Microsoft.OpenApi.Hidi --prerelease
2725

26+
```bash
27+
dotnet new tool-manifest #if you are setting up the OpenAPI.NET repo
28+
dotnet tool install --local Microsoft.OpenApi.Hidi --prerelease
29+
```
2830

2931

3032

3133
## How to use Hidi
3234

33-
Once you've installed the package locally, you can invoke the Hidi by running: hidi [command].
34-
You can access the list of command options we have by running hidi -h
35+
Once you've installed the package locally, you can invoke the Hidi by running: `hidi [command]`. You can access the list of command options we have by running `hidi -h`
3536
The tool avails the following commands:
3637

3738
• Validate
@@ -57,63 +58,84 @@ It accepts the following command:
5758
• --loglevel(-ll) - The log level to use when logging messages to the main output
5859
5960

60-
**Example:** `hidi.exe validate --openapi C:\OpenApidocs\Mail.yml --loglevel trace`
61+
#### Example:
6162

62-
Run validate -h to see the options available.
63+
```bash
64+
hidi validate --openapi C:\OpenApidocs\Mail.yml --loglevel trace`
65+
```
66+
67+
> Run `hidi validate -h` to see the options available.
6368

6469
### Transform
6570

6671
Used to convert file formats from JSON to YAML and vice versa and performs slicing of OpenAPI documents.
6772

6873
This command accepts the following parameters:
6974

70-
• --openapi(-d) - OpenAPI description file path in the local filesystem or a valid URL hosted on a HTTPS server
71-
• --csdl(--cs) - CSDL file path in the local filesystem or a valid URL hosted on a HTTPS server
72-
• --csdlfilter(--csf) - a filter parameter that a user can use to select a subset of a large CSDL file. They do so by providing a comma delimited list of EntitySet and Singleton names that appear in the EntityContainer.
73-
• --output(-o) - Output directory path for the transformed document.
74-
• --output-folder(--of) - The output directory path for the generated files.
75-
• --clean-ouput(--co) - an optional param that allows a user to overwrite an existing file.
76-
• --version(-v) - OpenAPI specification version.
77-
• --metadata-version(--mv) - the metadata version to use.
78-
• --format(-f) - File format
79-
• --terse-output(--to) - Produce terse json output
80-
• --settings-path(--sp) - The configuration file with CSDL conversion settings.
81-
• --loglevel(--ll) - The log level to use when logging messages to the main output
82-
• --inline-local - Inline local $ref instances
83-
• --inline-external(--ex) - Inline external $refs
84-
• --filterByOperationIds(--op) - Slice document based on OperationId(s) provided. Accepts a comma delimited list of operation ids.
85-
• --filterByTags(-t) - Slice document based on tag(s) provided. Accepts a comma delimited list of tags.
86-
• --filterByCollection(-c) - Slices the OpenAPI document based on the Postman Collection file generated by Resource Explorer
87-
• --manifest (-m) - Slices the OpenAPI document based on the requests defined in the API Manifest file referenced by the provided URI. For API manifests with multiple API Dependenties, use a fragment identifier to select the desired one. e.g ./apimanifest.json#example
75+
76+
• --openapi, (-d) - OpenAPI description file path in the local filesystem or a valid URL hosted on a HTTPS server
77+
• --csdl (--cs) - CSDL file path in the local filesystem or a valid URL hosted on a HTTPS server
78+
• --csdl-filter (--csf) - a filter parameter that a user can use to select a subset of a large CSDL file. They do so by providing a comma delimited list of EntitySet and Singleton names that appear in the EntityContainer.
79+
• --output (-o) - Output directory path for the transformed document.
80+
• --clean-output (--co) - an optional param that allows a user to overwrite an existing file.
81+
• --version (-v) - OpenAPI specification version.
82+
• --metadata-version (--mv) - the metadata version to use.
83+
• --format (-f) - File format
84+
• --terse-output (--to) - Produce terse json output
85+
• --settings-path (--sp) - The configuration file with CSDL conversion settings.
86+
• --log-level (--ll) - The log level to use when logging messages to the main output
87+
• --inline-local (--il) - Inline local $ref instances
88+
• --inline-external (--ie) - Inline external $refs instances
89+
• --filter-by-operationids(--op) - Slice document based on OperationId(s) provided. Accepts a comma delimited list of operation ids.
90+
• --filter-by-tags (--t) - Slice document based on tag(s) provided. Accepts a comma delimited list of tags.
91+
• --filter-by-collection (-c) - Slices the OpenAPI document based on the Postman Collection file generated by Resource Explorer
8892

89-
**Examples:**
93+
#### Examples:
9094

91-
1. Filtering by OperationIds
92-
hidi transform -d files\People.yml -f yaml -o files\People.yml -v OpenApi3_0 --op users_UpdateInsights --co
93-
94-
2. Filtering by Postman collection
95-
hidi transform --openapi files\People.yml --format yaml --output files\People2.yml --version OpenApi3_0 --filterByCollection Graph-Collection-0017059134807617005.postman_collection.json
96-
97-
3. CSDL--->OpenAPI conversion and filtering
98-
hidi transform --csdl Files/Todo.xml --output Files/Todo-subset.yml --format yaml --version OpenApi3_0 --filterByOperationIds Todos.Todo.UpdateTodo
99-
100-
4. CSDL Filtering by EntitySets and Singletons
101-
hidi transform --cs dataverse.csdl --csdlFilter "appointments,opportunities" -o appointmentsAndOpportunities.yaml --ll trace
102-
103-
Run transform -h to see all the available usage options.
95+
1. Filtering by OperationIds
96+
97+
```bash
98+
hidi transform -d files\People.yml -f yaml -o files\People.yml -v OpenApi3_0 --op users_UpdateInsights --co
99+
```
100+
101+
2. Filtering by Postman collection
102+
103+
```bash
104+
hidi transform --openapi files\People.yml --format yaml --output files\People2.yml --version OpenApi3_0 --filter-by-collection Graph-Collection-0017059134807617005.postman_collection.json
105+
```
106+
107+
3. CSDL--->OpenAPI conversion and filtering
108+
109+
```bash
110+
hidi transform --csdl Files/Todo.xml --output Files/Todo-subset.yml --format yaml --version OpenApi3_0 --filter-by-operationids Todos.Todo.UpdateTodo
111+
```
112+
113+
4. CSDL Filtering by EntitySets and Singletons
114+
115+
```bash
116+
hidi transform --cs dataverse.csdl --csdl-filter "appointments,opportunities" -o appointmentsAndOpportunities.yaml --ll trace
117+
```
118+
119+
> Run `hidi transform -h` to see all the available usage options.
104120

105121
### Show
106122

107123
This command accepts an OpenAPI document as an input parameter and generates a Markdown file that contains a diagram of the API using Mermaid syntax.
108124

109-
**Examples:**
125+
#### Examples:
110126

111-
1. hidi show -d files\People.yml -o People.md -ll trace
127+
```bash
128+
hidi show -d files\People.yml -o People.md -ll trace
129+
```
112130

113131
### Plugin
114132

115133
This command generates an OpenAI style Plugin manifest and minimal OpenAPI file based on the provided API Manifest
116134

117-
**Examples:**
135+
#### Examples:
136+
137+
```bash
138+
hidi plugin -m exampleApiManifest.yml -o mypluginfolder
139+
```
118140

119-
1. hidi plugin -m exampleApiManifest.yml -o mypluginfolder
141+
> Run `hidi plugin -h` to see all the available usage options.

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
using Microsoft.OpenApi.Extensions;
1212
using Microsoft.OpenApi.Models;
1313
using Microsoft.OpenApi.Reader;
14-
using Microsoft.OpenApi.Readers;
1514
using Microsoft.OpenApi.Services;
1615
using Microsoft.OpenApi.Validations;
16+
using Microsoft.OpenApi.Writers;
1717

1818
namespace Microsoft.OpenApi.Workbench
1919
{
@@ -242,7 +242,7 @@ internal async Task ParseDocumentAsync()
242242
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
243243
}
244244

245-
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName());
245+
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName().ToLowerInvariant(), settings);
246246
var document = readResult.Document;
247247
var context = readResult.Diagnostic;
248248

@@ -298,13 +298,13 @@ internal async Task ParseDocumentAsync()
298298
/// </summary>
299299
private async Task<string> WriteContentsAsync(OpenApiDocument document)
300300
{
301-
var outputStream = new MemoryStream();
301+
using var outputStream = new MemoryStream();
302302

303303
await document.SerializeAsync(
304304
outputStream,
305305
Version,
306306
Format,
307-
(Writers.OpenApiWriterSettings)new()
307+
new OpenApiWriterSettings()
308308
{
309309
InlineLocalReferences = InlineLocal,
310310
InlineExternalReferences = InlineExternal

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiCompoentsTests.cs renamed to test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiComponentsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
1010
{
11-
public class OpenApiCompoentsTests
11+
public class OpenApiComponentsTests
1212
{
1313
[Theory]
1414
[InlineData("./FirstLevel/SecondLevel/ThridLevel/File.json#/components/schemas/ExternalRelativePathModel", "ExternalRelativePathModel", "./FirstLevel/SecondLevel/ThridLevel/File.json")]

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,5 +575,20 @@ public void ParseEmptyMemoryStreamThrowsAnArgumentException()
575575
{
576576
Assert.Throws<ArgumentException>(() => OpenApiDocument.Load(new MemoryStream()));
577577
}
578+
579+
[Fact]
580+
public async Task ValidateReferencedExampleInSchemaWorks()
581+
{
582+
// Arrange && Act
583+
var path = Path.Combine(SampleFolderPath, "docWithReferencedExampleInSchemaWorks.yaml");
584+
var result = await OpenApiDocument.LoadAsync(path, SettingsFixture.ReaderSettings);
585+
var actualSchemaExample = result.Document.Components.Schemas["DiffCreatedEvent"].Properties["updatedAt"].Example;
586+
var targetSchemaExample = result.Document.Components.Schemas["Timestamp"].Example;
587+
588+
// Assert
589+
Assert.Equal(targetSchemaExample, actualSchemaExample);
590+
Assert.Empty(result.Diagnostic.Errors);
591+
Assert.Empty(result.Diagnostic.Warnings);
592+
}
578593
}
579594
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: 3.1.1
2+
info:
3+
title: ReferenceById
4+
version: 1.0.0
5+
paths:
6+
/resource:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/DiffCreatedEvent'
15+
components:
16+
schemas:
17+
DiffCreatedEvent:
18+
description: 'diff index created'
19+
type: object
20+
additionalProperties: false
21+
properties:
22+
updatedAt:
23+
$ref: '#/components/schemas/Timestamp'
24+
example:
25+
"updatedAt": '2020-06-30T06:43:51.391Z'
26+
Timestamp:
27+
type: string
28+
format: date-time
29+
description: 'timestamp'
30+
example: '2020-06-30T06:43:51.391Z'

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
using System.Globalization;
77
using System.IO;
88
using System.Linq;
9+
using System.Net;
910
using System.Net.Http;
11+
using System.Net.Http.Headers;
1012
using System.Text;
1113
using System.Text.Json.Nodes;
14+
using System.Threading;
1215
using System.Threading.Tasks;
1316
using FluentAssertions;
1417
using Microsoft.OpenApi.Extensions;
@@ -21,6 +24,8 @@
2124
using Microsoft.OpenApi.Validations;
2225
using Microsoft.OpenApi.Validations.Rules;
2326
using Microsoft.OpenApi.Writers;
27+
using Moq;
28+
using Moq.Protected;
2429
using Xunit;
2530

2631
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -29,8 +34,6 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
2934
public class OpenApiDocumentTests
3035
{
3136
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/";
32-
private const string codacyApi = "https://api.codacy.com/api/api-docs/swagger.yaml";
33-
3437
private static async Task<T> CloneAsync<T>(T element) where T : class, IOpenApiSerializable
3538
{
3639
using var stream = new MemoryStream();
@@ -1494,8 +1497,24 @@ public async Task ParseDocumentWithExampleReferencesPasses()
14941497
[Fact]
14951498
public async Task ParseDocumentWithNonStandardMIMETypePasses()
14961499
{
1500+
var path = Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml");
1501+
using var stream = Resources.GetStream(path);
1502+
using var streamReader = new StreamReader(stream);
1503+
var contentAsString = await streamReader.ReadToEndAsync();
1504+
var mockMessageHandler = new Mock<HttpMessageHandler>();
1505+
mockMessageHandler.Protected()
1506+
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
1507+
.ReturnsAsync(new HttpResponseMessage {
1508+
StatusCode = HttpStatusCode.OK,
1509+
Content = new StringContent(contentAsString, new MediaTypeHeaderValue("text/x-yaml"))
1510+
});
1511+
var settings = new OpenApiReaderSettings
1512+
{
1513+
HttpClient = new HttpClient(mockMessageHandler.Object)
1514+
};
1515+
settings.AddYamlReader();
14971516
// Act & Assert: Ensure NotSupportedException is not thrown for non-standard MIME type: text/x-yaml
1498-
var result = await OpenApiDocument.LoadAsync(codacyApi, SettingsFixture.ReaderSettings);
1517+
var result = await OpenApiDocument.LoadAsync("https://localhost/doesntmatter/foo.bar", settings);
14991518
Assert.NotNull(result.Document);
15001519
}
15011520
}

test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
1515
<PackageReference Include="Moq" Version="4.20.72" />
1616
<PackageReference Include="SharpYaml" Version="2.1.1" />
17-
<PackageReference Include="Verify.Xunit" Version="28.13.0" />
17+
<PackageReference Include="Verify.Xunit" Version="28.14.1" />
1818
<PackageReference Include="xunit" Version="2.9.3" />
1919
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" PrivateAssets="all" />
2020
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

0 commit comments

Comments
 (0)