Skip to content

Commit 2f8ea28

Browse files
Merge branch 'main' into feat/enable-NRT
2 parents f608cea + 371a574 commit 2f8ea28

File tree

52 files changed

+2018
-440
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2018
-440
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ var document = new OpenApiDocument
5555
{
5656
["/pets"] = new OpenApiPathItem
5757
{
58-
Operations = new Dictionary<OperationType, OpenApiOperation>
58+
Operations = new Dictionary<HttpMethod, OpenApiOperation>
5959
{
60-
[OperationType.Get] = new OpenApiOperation
60+
[HttpMethod.Get] = new OpenApiOperation
6161
{
6262
Description = "Returns all pets from the system that the user has access to",
6363
Responses = new OpenApiResponses

docs/images/workbench.png

-33 KB
Loading

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Text;
56
using System.Text.RegularExpressions;
67
using Humanizer;
@@ -53,11 +54,11 @@ public override void Visit(IOpenApiSchema schema)
5354

5455
public override void Visit(IOpenApiPathItem pathItem)
5556
{
56-
if (pathItem.Operations is not null && pathItem.Operations.TryGetValue(OperationType.Put, out var value) &&
57+
if (pathItem.Operations is not null && pathItem.Operations.TryGetValue(HttpMethod.Put, out var value) &&
5758
value.OperationId != null)
5859
{
5960
var operationId = value.OperationId;
60-
pathItem.Operations[OperationType.Put].OperationId = ResolvePutOperationId(operationId);
61+
pathItem.Operations[HttpMethod.Put].OperationId = ResolvePutOperationId(operationId);
6162
}
6263

6364
base.Visit(pathItem);

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ private static async Task WriteOpenApiAsync(HidiOptions options, OpenApiFormat o
261261
return document;
262262
}
263263

264-
private static Func<string, OperationType?, OpenApiOperation, bool>? FilterOpenApiDocument(string? filterByOperationIds, string? filterByTags, Dictionary<string, List<string>> requestUrls, OpenApiDocument document, ILogger logger)
264+
private static Func<string, HttpMethod, OpenApiOperation, bool>? FilterOpenApiDocument(string? filterByOperationIds, string? filterByTags, Dictionary<string, List<string>> requestUrls, OpenApiDocument document, ILogger logger)
265265
{
266-
Func<string, OperationType?, OpenApiOperation, bool>? predicate = null;
266+
Func<string, HttpMethod, OpenApiOperation, bool>? predicate = null;
267267

268268
using (logger.BeginScope("Create Filter"))
269269
{

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

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiPathItem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System.Collections.Generic;
3+
using System.Net.Http;
34
using Microsoft.OpenApi.Interfaces;
45

56
namespace Microsoft.OpenApi.Models.Interfaces;
@@ -13,7 +14,7 @@ public interface IOpenApiPathItem : IOpenApiDescribedElement, IOpenApiSummarized
1314
/// <summary>
1415
/// Gets the definition of operations on this path.
1516
/// </summary>
16-
public IDictionary<OperationType, OpenApiOperation>? Operations { get; }
17+
public IDictionary<HttpMethod, OpenApiOperation> Operations { get; }
1718

1819
/// <summary>
1920
/// An alternative server array to service all operations in this path.

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,35 @@ public OpenApiDocument(OpenApiDocument? document)
147147
BaseUri = document?.BaseUri != null ? document.BaseUri : new(OpenApiConstants.BaseRegistryUri + Guid.NewGuid());
148148
}
149149

150+
/// <summary>
151+
/// Serialize <see cref="OpenApiDocument"/> to an Open API document using the specified version.
152+
/// </summary>
153+
/// <param name="version">The Open API specification version to serialize the document as.</param>
154+
/// <param name="writer">The <see cref="IOpenApiWriter"/> to serialize the document to.</param>
155+
/// <exception cref="ArgumentOutOfRangeException">
156+
/// <paramref name="version"/> is not a supported Open API specification version.
157+
/// </exception>
158+
public void SerializeAs(OpenApiSpecVersion version, IOpenApiWriter writer)
159+
{
160+
switch (version)
161+
{
162+
case OpenApiSpecVersion.OpenApi2_0:
163+
SerializeAsV2(writer);
164+
break;
165+
166+
case OpenApiSpecVersion.OpenApi3_0:
167+
SerializeAsV3(writer);
168+
break;
169+
170+
case OpenApiSpecVersion.OpenApi3_1:
171+
SerializeAsV31(writer);
172+
break;
173+
174+
default:
175+
throw new ArgumentOutOfRangeException(nameof(version), version, string.Format(Properties.SRResource.OpenApiSpecVersionNotSupported, version));
176+
}
177+
}
178+
150179
/// <summary>
151180
/// Serialize <see cref="OpenApiDocument"/> to Open API v3.1 document.
152181
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Net.Http;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models.Interfaces;
@@ -22,8 +23,8 @@ public class OpenApiPathItem : IOpenApiExtensible, IOpenApiReferenceable, IOpenA
2223
public string? Description { get; set; }
2324

2425
/// <inheritdoc/>
25-
public IDictionary<OperationType, OpenApiOperation>? Operations { get; set; }
26-
= new Dictionary<OperationType, OpenApiOperation>();
26+
public IDictionary<HttpMethod, OpenApiOperation>? Operations { get; set; }
27+
= new Dictionary<HttpMethod, OpenApiOperation>();
2728

2829
/// <inheritdoc/>
2930
public IList<OpenApiServer>? Servers { get; set; } = [];
@@ -39,7 +40,7 @@ public class OpenApiPathItem : IOpenApiExtensible, IOpenApiReferenceable, IOpenA
3940
/// </summary>
4041
/// <param name="operationType">The operation type kind.</param>
4142
/// <param name="operation">The operation item.</param>
42-
public void AddOperation(OperationType operationType, OpenApiOperation operation)
43+
public void AddOperation(HttpMethod operationType, OpenApiOperation operation)
4344
{
4445
if (Operations is not null)
4546
{
@@ -60,7 +61,7 @@ internal OpenApiPathItem(IOpenApiPathItem pathItem)
6061
Utils.CheckArgumentNull(pathItem);
6162
Summary = pathItem.Summary ?? Summary;
6263
Description = pathItem.Description ?? Description;
63-
Operations = pathItem.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
64+
Operations = pathItem.Operations != null ? new Dictionary<HttpMethod, OpenApiOperation>(pathItem.Operations) : null;
6465
Servers = pathItem.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
6566
Parameters = pathItem.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
6667
Extensions = pathItem.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
@@ -97,13 +98,13 @@ public void SerializeAsV2(IOpenApiWriter writer)
9798
{
9899
foreach (var operation in Operations)
99100
{
100-
if (operation.Key != OperationType.Trace)
101-
{
102-
writer.WriteOptionalObject(
103-
operation.Key.GetDisplayName(),
104-
operation.Value,
105-
(w, o) => o.SerializeAsV2(w));
106-
}
101+
if (operation.Key != HttpMethod.Trace)
102+
{
103+
writer.WriteOptionalObject(
104+
operation.Key.Method.ToLowerInvariant(),
105+
operation.Value,
106+
(w, o) => o.SerializeAsV2(w));
107+
}
107108
}
108109
}
109110

@@ -143,9 +144,9 @@ internal virtual void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersio
143144
foreach (var operation in Operations)
144145
{
145146
writer.WriteOptionalObject(
146-
operation.Key.GetDisplayName(),
147-
operation.Value,
148-
callback);
147+
operation.Key.Method.ToLowerInvariant(),
148+
operation.Value,
149+
callback);
149150
}
150151
}
151152

0 commit comments

Comments
 (0)