Skip to content

Commit d37e5de

Browse files
committed
feat: openapiformat enum cleanup
1 parent 25136af commit d37e5de

File tree

18 files changed

+69
-94
lines changed

18 files changed

+69
-94
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var stream = await httpClient.GetStreamAsync("main/examples/v3.0/petstore.yaml")
8888
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
8989

9090
// Write V2 as JSON
91-
var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);
91+
var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiConstants.Json);
9292

9393
```
9494

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog
5555
if (options.Output == null)
5656
{
5757
#pragma warning disable CA1308 // Normalize strings to uppercase
58-
var extension = options.OpenApiFormat?.GetDisplayName().ToLowerInvariant();
58+
var extension = options.OpenApiFormat?.ToLowerInvariant();
5959
var inputExtension = !string.IsNullOrEmpty(extension) ? string.Concat(".", extension)
6060
: GetInputPathExtension(options.OpenApi, options.Csdl);
6161

@@ -73,7 +73,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog
7373
}
7474

7575
// Default to yaml and OpenApiVersion 3_1 during csdl to OpenApi conversion
76-
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml);
76+
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiConstants.Yaml);
7777
var openApiVersion = options.Version != null ? TryParseOpenApiSpecVersion(options.Version) : OpenApiSpecVersion.OpenApi3_1;
7878

7979
// If ApiManifest is provided, set the referenced OpenAPI document
@@ -92,7 +92,7 @@ public static async Task TransformOpenApiDocumentAsync(HidiOptions options, ILog
9292
}
9393

9494
// Load OpenAPI document
95-
var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
95+
var document = await GetOpenApiAsync(options, openApiFormat, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
9696

9797
if (options.FilterOptions != null && document is not null)
9898
{
@@ -189,7 +189,7 @@ private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger,
189189
return document;
190190
}
191191

192-
private static async Task WriteOpenApiAsync(HidiOptions options, OpenApiFormat openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger, CancellationToken cancellationToken)
192+
private static async Task WriteOpenApiAsync(HidiOptions options, string openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger, CancellationToken cancellationToken)
193193
{
194194
using (logger.BeginScope("Output"))
195195
{
@@ -205,8 +205,8 @@ private static async Task WriteOpenApiAsync(HidiOptions options, OpenApiFormat o
205205

206206
IOpenApiWriter writer = openApiFormat switch
207207
{
208-
OpenApiFormat.Json => options.TerseOutput ? new(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
209-
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
208+
OpenApiConstants.Json => options.TerseOutput ? new(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
209+
OpenApiConstants.Yaml => new OpenApiYamlWriter(textWriter, settings),
210210
_ => throw new ArgumentException("Unknown format"),
211211
};
212212

@@ -560,10 +560,10 @@ SecurityException or
560560
/// <param name="input"></param>
561561
/// <param name="logger"></param>
562562
/// <returns></returns>
563-
private static OpenApiFormat GetOpenApiFormat(string input, ILogger logger)
563+
private static string GetOpenApiFormat(string input, ILogger logger)
564564
{
565565
logger.LogTrace("Getting the OpenApi format");
566-
return !input.StartsWith("http", StringComparison.OrdinalIgnoreCase) && Path.GetExtension(input) == ".json" ? OpenApiFormat.Json : OpenApiFormat.Yaml;
566+
return !input.StartsWith("http", StringComparison.OrdinalIgnoreCase) && Path.GetExtension(input) == ".json" ? OpenApiConstants.Json : OpenApiConstants.Yaml;
567567
}
568568

569569
private static string GetInputPathExtension(string? openapi = null, string? csdl = null)
@@ -590,8 +590,8 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
590590
throw new ArgumentException("Please input a file path or URL");
591591
}
592592

593-
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml);
594-
var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, null, cancellationToken).ConfigureAwait(false);
593+
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi) ? GetOpenApiFormat(options.OpenApi, logger) : OpenApiConstants.Yaml);
594+
var document = await GetOpenApiAsync(options, openApiFormat, logger, null, cancellationToken).ConfigureAwait(false);
595595
if (document is not null)
596596
{
597597
using (logger.BeginScope("Creating diagram"))
@@ -754,10 +754,10 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg
754754
}
755755

756756
var openApiFormat = options.OpenApiFormat ?? (!string.IsNullOrEmpty(options.OpenApi)
757-
? GetOpenApiFormat(options.OpenApi, logger) : OpenApiFormat.Yaml);
757+
? GetOpenApiFormat(options.OpenApi, logger) : OpenApiConstants.Yaml);
758758

759759
// Load OpenAPI document
760-
var document = await GetOpenApiAsync(options, openApiFormat.GetDisplayName(), logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
760+
var document = await GetOpenApiAsync(options, openApiFormat, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
761761

762762
cancellationToken.ThrowIfCancellationRequested();
763763

@@ -777,7 +777,7 @@ internal static async Task PluginManifestAsync(HidiOptions options, ILogger logg
777777
options.TerseOutput = true;
778778
if (document is not null)
779779
{
780-
await WriteOpenApiAsync(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_1, document, logger, cancellationToken).ConfigureAwait(false);
780+
await WriteOpenApiAsync(options, OpenApiConstants.Json, OpenApiSpecVersion.OpenApi3_1, document, logger, cancellationToken).ConfigureAwait(false);
781781

782782
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
783783
var manifest = new OpenAIPluginManifest(document.Info.Title ?? "Title",

src/Microsoft.OpenApi.Hidi/Options/CommandOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class CommandOptions
1616
public readonly Option<bool> CleanOutputOption = new("--clean-output", "Overwrite an existing file");
1717
public readonly Option<string> VersionOption = new("--version", "OpenAPI specification version");
1818
public readonly Option<string> MetadataVersionOption = new("--metadata-version", "Graph metadata version to use.");
19-
public readonly Option<OpenApiFormat?> FormatOption = new("--format", "File format");
19+
public readonly Option<string?> FormatOption = new("--format", "File format");
2020
public readonly Option<bool> TerseOutputOption = new("--terse-output", "Produce terse json output");
2121
public readonly Option<string> SettingsFileOption = new("--settings-path", "The configuration file with CSDL conversion settings.");
2222
public readonly Option<LogLevel> LogLevelOption = new("--log-level", () => LogLevel.Information, "The log level to use when logging messages to the main output.");

src/Microsoft.OpenApi.Hidi/Options/HidiOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class HidiOptions
2020
public bool CleanOutput { get; set; }
2121
public string? Version { get; set; }
2222
public string? MetadataVersion { get; set; }
23-
public OpenApiFormat? OpenApiFormat { get; set; }
23+
public string? OpenApiFormat { get; set; }
2424
public bool TerseOutput { get; set; }
2525
public IConfiguration? SettingsConfig { get; set; }
2626
public LogLevel LogLevel { get; set; }

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -43,7 +43,7 @@ public class MainModel : INotifyPropertyChanged
4343
/// <summary>
4444
/// Default format.
4545
/// </summary>
46-
private OpenApiFormat _format = OpenApiFormat.Yaml;
46+
private string _format = OpenApiConstants.Yaml;
4747

4848
/// <summary>
4949
/// Default version.
@@ -121,7 +121,7 @@ public string RenderTime
121121
}
122122
}
123123

124-
public OpenApiFormat Format
124+
public string Format
125125
{
126126
get => _format;
127127
set
@@ -166,14 +166,14 @@ public OpenApiSpecVersion Version
166166

167167
public bool IsYaml
168168
{
169-
get => Format == OpenApiFormat.Yaml;
170-
set => Format = value ? OpenApiFormat.Yaml : Format;
169+
get => Format == OpenApiConstants.Yaml;
170+
set => Format = value ? OpenApiConstants.Yaml : Format;
171171
}
172172

173173
public bool IsJson
174174
{
175-
get => Format == OpenApiFormat.Json;
176-
set => Format = value ? OpenApiFormat.Json : Format;
175+
get => Format == OpenApiConstants.Json;
176+
set => Format = value ? OpenApiConstants.Json : Format;
177177
}
178178

179179
public bool IsV2_0
@@ -243,7 +243,7 @@ internal async Task ParseDocumentAsync()
243243
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
244244
}
245245

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

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Microsoft.OpenApi.Exceptions;
99
using Microsoft.OpenApi.Interfaces;
10+
using Microsoft.OpenApi.Models;
1011
using Microsoft.OpenApi.Properties;
1112
using Microsoft.OpenApi.Writers;
1213

@@ -28,7 +29,7 @@ public static class OpenApiSerializableExtensions
2829
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
2930
where T : IOpenApiSerializable
3031
{
31-
return element.SerializeAsync(stream, specVersion, OpenApiFormat.Json, cancellationToken);
32+
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Json, cancellationToken);
3233
}
3334

3435
/// <summary>
@@ -42,7 +43,7 @@ public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenAp
4243
public static Task SerializeAsYamlAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
4344
where T : IOpenApiSerializable
4445
{
45-
return element.SerializeAsync(stream, specVersion, OpenApiFormat.Yaml, cancellationToken);
46+
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Yaml, cancellationToken);
4647
}
4748

4849
/// <summary>
@@ -59,7 +60,7 @@ public static Task SerializeAsync<T>(
5960
this T element,
6061
Stream stream,
6162
OpenApiSpecVersion specVersion,
62-
OpenApiFormat format,
63+
string format,
6364
CancellationToken cancellationToken = default)
6465
where T : IOpenApiSerializable
6566
{
@@ -81,7 +82,7 @@ public static Task SerializeAsync<T>(
8182
this T element,
8283
Stream stream,
8384
OpenApiSpecVersion specVersion,
84-
OpenApiFormat format,
85+
string format,
8586
OpenApiWriterSettings? settings = null,
8687
CancellationToken cancellationToken = default)
8788
where T : IOpenApiSerializable
@@ -92,8 +93,8 @@ public static Task SerializeAsync<T>(
9293

9394
IOpenApiWriter writer = format switch
9495
{
95-
OpenApiFormat.Json => new OpenApiJsonWriter(streamWriter, settings, false),
96-
OpenApiFormat.Yaml => new OpenApiYamlWriter(streamWriter, settings),
96+
OpenApiConstants.Json => new OpenApiJsonWriter(streamWriter, settings, false),
97+
OpenApiConstants.Yaml => new OpenApiYamlWriter(streamWriter, settings),
9798
_ => throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)),
9899
};
99100
return element.SerializeAsync(writer, specVersion, cancellationToken);
@@ -147,7 +148,7 @@ public static Task<string> SerializeAsJsonAsync<T>(
147148
CancellationToken cancellationToken = default)
148149
where T : IOpenApiSerializable
149150
{
150-
return element.SerializeAsync(specVersion, OpenApiFormat.Json, cancellationToken);
151+
return element.SerializeAsync(specVersion, OpenApiConstants.Json, cancellationToken);
151152
}
152153

153154
/// <summary>
@@ -163,7 +164,7 @@ public static Task<string> SerializeAsYamlAsync<T>(
163164
CancellationToken cancellationToken = default)
164165
where T : IOpenApiSerializable
165166
{
166-
return element.SerializeAsync(specVersion, OpenApiFormat.Yaml, cancellationToken);
167+
return element.SerializeAsync(specVersion, OpenApiConstants.Yaml, cancellationToken);
167168
}
168169

169170
/// <summary>
@@ -177,7 +178,7 @@ public static Task<string> SerializeAsYamlAsync<T>(
177178
public static async Task<string> SerializeAsync<T>(
178179
this T element,
179180
OpenApiSpecVersion specVersion,
180-
OpenApiFormat format,
181+
string format,
181182
CancellationToken cancellationToken = default)
182183
where T : IOpenApiSerializable
183184
{

src/Microsoft.OpenApi/OpenApiFormat.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchF
261261
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
262262
CleanOutput = true,
263263
Version = "3.0",
264-
OpenApiFormat = OpenApiFormat.Yaml,
264+
OpenApiFormat = OpenApiConstants.Yaml,
265265
TerseOutput = false,
266266
InlineLocal = false,
267267
InlineExternal = false,
@@ -296,7 +296,7 @@ public async Task TransformToPowerShellCompliantOpenApiAsync()
296296
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml"),
297297
CleanOutput = true,
298298
Version = "3.0",
299-
OpenApiFormat = OpenApiFormat.Yaml,
299+
OpenApiFormat = OpenApiConstants.Yaml,
300300
TerseOutput = false,
301301
InlineLocal = false,
302302
InlineExternal = false,

test/Microsoft.OpenApi.Tests/Models/OpenApiContactTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public class OpenApiContactTests
2828
};
2929

3030
[Theory]
31-
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json, "{ }")]
32-
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json, "{ }")]
33-
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml, "{ }")]
34-
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml, "{ }")]
31+
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Json, "{ }")]
32+
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiConstants.Json, "{ }")]
33+
[InlineData(OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, "{ }")]
34+
[InlineData(OpenApiSpecVersion.OpenApi2_0, OpenApiConstants.Yaml, "{ }")]
3535
public async Task SerializeBasicContactWorks(
3636
OpenApiSpecVersion version,
37-
OpenApiFormat format,
37+
string format,
3838
string expected)
3939
{
4040
// Arrange & Act

test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ public async Task SerializeDocumentWithReferenceButNoComponents()
15571557
document.Paths["/"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema = new OpenApiSchemaReference("test", document);
15581558

15591559
// Act
1560-
var actual = await document.SerializeAsync(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);
1560+
var actual = await document.SerializeAsync(OpenApiSpecVersion.OpenApi2_0, OpenApiConstants.Json);
15611561

15621562
// Assert
15631563
Assert.NotEmpty(actual);

0 commit comments

Comments
 (0)