diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 43ea50e2f..ec86e0b7f 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,13 +30,13 @@ jobs: id: getversion - name: Push to GitHub Packages - Nightly if: ${{ github.ref == 'refs/heads/vnext' }} - uses: docker/build-push-action@v3.1.0 + uses: docker/build-push-action@v3.1.1 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly - name: Push to GitHub Packages - Release if: ${{ github.ref == 'refs/heads/master' }} - uses: docker/build-push-action@v3.1.0 + uses: docker/build-push-action@v3.1.1 with: push: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }} diff --git a/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj b/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj index fb6eaecc1..6045d85b6 100644 --- a/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj +++ b/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj @@ -9,9 +9,9 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/README.md b/README.md index 9405526bf..0190e572d 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Project Objectives # Installation -- Install core Nuget package `Microsoft.OpenApi` -- Install readers Nuget package `Microsoft.OpenApi.Readers` +- Install core Nuget package [**Microsoft.OpenApi**](https://www.nuget.org/packages/Microsoft.OpenApi) +- Install readers Nuget package [**Microsoft.OpenApi.Readers**](https://www.nuget.org/packages/Microsoft.OpenApi.Readers) # Processors The OpenAPI.NET project holds the base object model for representing OpenAPI documents as .NET objects. Some developers have found the need to write processors that convert other data formats into this OpenAPI.NET object model. We'd like to curate that list of processors in this section of the readme. diff --git a/src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs b/src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs new file mode 100644 index 000000000..e8d9431de --- /dev/null +++ b/src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs @@ -0,0 +1,73 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; + +namespace Microsoft.OpenApi.Hidi.Handlers +{ + internal class TransformCommandHandler : ICommandHandler + { + public Option DescriptionOption { get; set; } + public Option CsdlOption { get; set; } + public Option CsdlFilterOption { get; set; } + public Option OutputOption { get; set; } + public Option CleanOutputOption { get; set; } + public Option VersionOption { get; set; } + public Option FormatOption { get; set; } + public Option TerseOutputOption { get; set; } + public Option LogLevelOption { get; set; } + public Option FilterByOperationIdsOption { get; set; } + public Option FilterByTagsOption { get; set; } + public Option FilterByCollectionOption { get; set; } + public Option InlineLocalOption { get; set; } + public Option InlineExternalOption { get; set; } + + public int Invoke(InvocationContext context) + { + return InvokeAsync(context).GetAwaiter().GetResult(); + } + public async Task InvokeAsync(InvocationContext context) + { + string openapi = context.ParseResult.GetValueForOption(DescriptionOption); + string csdlFilter = context.ParseResult.GetValueForOption(CsdlFilterOption); + string csdl = context.ParseResult.GetValueForOption(CsdlOption); + FileInfo output = context.ParseResult.GetValueForOption(OutputOption); + bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption); + string? version = context.ParseResult.GetValueForOption(VersionOption); + OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption); + bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption); + LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption); + bool inlineLocal = context.ParseResult.GetValueForOption(InlineLocalOption); + bool inlineExternal = context.ParseResult.GetValueForOption(InlineExternalOption); + string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption); + string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption); + string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption); + CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken)); + + using var loggerFactory = Logger.ConfigureLogger(logLevel); + var logger = loggerFactory.CreateLogger(); + try + { + await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, logLevel, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, cancellationToken); + + return 0; + } + catch (Exception ex) + { +#if DEBUG + logger.LogCritical(ex, ex.Message); + throw; // so debug tools go straight to the source of the exception when attached +#else + logger.LogCritical( ex.Message); + return 1; +#endif + } + } + } +} diff --git a/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs b/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs new file mode 100644 index 000000000..2faa771ea --- /dev/null +++ b/src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; + +namespace Microsoft.OpenApi.Hidi.Handlers +{ + internal class ValidateCommandHandler : ICommandHandler + { + public Option DescriptionOption { get; set; } + public Option LogLevelOption { get; set; } + + public int Invoke(InvocationContext context) + { + return InvokeAsync(context).GetAwaiter().GetResult(); + } + public async Task InvokeAsync(InvocationContext context) + { + string openapi = context.ParseResult.GetValueForOption(DescriptionOption); + LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption); + CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken)); + + + using var loggerFactory = Logger.ConfigureLogger(logLevel); + var logger = loggerFactory.CreateLogger(); + try + { + await OpenApiService.ValidateOpenApiDocument(openapi, logLevel, cancellationToken); + return 0; + } + catch (Exception ex) + { +#if DEBUG + logger.LogCritical(ex, ex.Message); + throw; // so debug tools go straight to the source of the exception when attached +#else + logger.LogCritical( ex.Message); + return 1; +#endif + } + } + } +} diff --git a/src/Microsoft.OpenApi.Hidi/Logger.cs b/src/Microsoft.OpenApi.Hidi/Logger.cs new file mode 100644 index 000000000..2b02e9600 --- /dev/null +++ b/src/Microsoft.OpenApi.Hidi/Logger.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.Extensions.Logging; + +namespace Microsoft.OpenApi.Hidi +{ + public class Logger + { + public static ILoggerFactory ConfigureLogger(LogLevel logLevel) + { + // Configure logger options +#if DEBUG + logLevel = logLevel > LogLevel.Debug ? LogLevel.Debug : logLevel; +#endif + + return LoggerFactory.Create((builder) => + { + builder + .AddSimpleConsole(c => + { + c.IncludeScopes = true; + }) +#if DEBUG + .AddDebug() +#endif + .SetMinimumLevel(logLevel); + }); + } + } +} diff --git a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj index f9e1f0d9e..eda11732d 100644 --- a/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj +++ b/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj @@ -41,8 +41,8 @@ - - + + diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index d9f9887e1..c37c9479d 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -26,7 +26,6 @@ using System.Threading; using System.Xml.Xsl; using System.Xml; -using System.Runtime.CompilerServices; using System.Reflection; namespace Microsoft.OpenApi.Hidi @@ -36,7 +35,7 @@ public class OpenApiService /// /// Implementation of the transform command /// - public static async Task TransformOpenApiDocument( + public static async Task TransformOpenApiDocument( string openapi, string csdl, string csdlFilter, @@ -45,7 +44,7 @@ public static async Task TransformOpenApiDocument( string? version, OpenApiFormat? format, bool terseOutput, - LogLevel loglevel, + LogLevel logLevel, bool inlineLocal, bool inlineExternal, string filterbyoperationids, @@ -54,9 +53,8 @@ public static async Task TransformOpenApiDocument( CancellationToken cancellationToken ) { - using var loggerFactory = ConfigureLoggerInstance(loglevel); + using var loggerFactory = Logger.ConfigureLogger(logLevel); var logger = loggerFactory.CreateLogger(); - try { if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl)) @@ -213,18 +211,11 @@ CancellationToken cancellationToken logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms"); textWriter.Flush(); } - return 0; } catch (Exception ex) { -#if DEBUG - logger.LogCritical(ex, ex.Message); -#else - logger.LogCritical(ex.Message); - -#endif - return 1; - } + throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex); + } } private static XslCompiledTransform GetFilterTransform() @@ -253,14 +244,13 @@ private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslC /// /// Implementation of the validate command /// - public static async Task ValidateOpenApiDocument( + public static async Task ValidateOpenApiDocument( string openapi, - LogLevel loglevel, + LogLevel logLevel, CancellationToken cancellationToken) { - using var loggerFactory = ConfigureLoggerInstance(loglevel); + using var loggerFactory = Logger.ConfigureLogger(logLevel); var logger = loggerFactory.CreateLogger(); - try { if (string.IsNullOrEmpty(openapi)) @@ -307,19 +297,11 @@ public static async Task ValidateOpenApiDocument( logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report.."); logger.LogInformation(statsVisitor.GetStatisticsReport()); } - - return 0; } catch (Exception ex) { -#if DEBUG - logger.LogCritical(ex, ex.Message); -#else - logger.LogCritical(ex.Message); -#endif - return 1; + throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex); } - } /// @@ -596,7 +578,7 @@ private static ILoggerFactory ConfigureLoggerInstance(LogLevel loglevel) loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel; #endif - return LoggerFactory.Create((builder) => { + return Microsoft.Extensions.Logging.LoggerFactory.Create((builder) => { builder .AddSimpleConsole(c => { c.IncludeScopes = true; diff --git a/src/Microsoft.OpenApi.Hidi/Program.cs b/src/Microsoft.OpenApi.Hidi/Program.cs index d19e48cf4..926c58deb 100644 --- a/src/Microsoft.OpenApi.Hidi/Program.cs +++ b/src/Microsoft.OpenApi.Hidi/Program.cs @@ -1,12 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -using System; using System.CommandLine; using System.IO; -using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Hidi.Handlers; namespace Microsoft.OpenApi.Hidi { @@ -66,7 +65,11 @@ static async Task Main(string[] args) logLevelOption }; - validateCommand.SetHandler(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption); + validateCommand.Handler = new ValidateCommandHandler + { + DescriptionOption = descriptionOption, + LogLevelOption = logLevelOption + }; var transformCommand = new Command("transform") { @@ -86,8 +89,23 @@ static async Task Main(string[] args) inlineExternalOption }; - transformCommand.SetHandler ( - OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption); + transformCommand.Handler = new TransformCommandHandler + { + DescriptionOption = descriptionOption, + CsdlOption = csdlOption, + CsdlFilterOption = csdlFilterOption, + OutputOption = outputOption, + CleanOutputOption = cleanOutputOption, + VersionOption = versionOption, + FormatOption = formatOption, + TerseOutputOption = terseOutputOption, + LogLevelOption = logLevelOption, + FilterByOperationIdsOption = filterByOperationIdsOption, + FilterByTagsOption = filterByTagsOption, + FilterByCollectionOption = filterByCollectionOption, + InlineLocalOption = inlineLocalOption, + InlineExternalOption = inlineExternalOption + }; rootCommand.Add(transformCommand); rootCommand.Add(validateCommand); diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj index 1ff5b99e9..c92eaf766 100644 --- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj +++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj @@ -10,7 +10,7 @@ Microsoft Microsoft.OpenApi.Readers Microsoft.OpenApi.Readers - 1.4.0-preview1 + 1.4.0-preview2 OpenAPI.NET Readers for JSON and YAML documents © Microsoft Corporation. All rights reserved. OpenAPI .NET diff --git a/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs b/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs index 920593bbd..db60448ea 100644 --- a/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs +++ b/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs @@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Attributes /// Represents the Open Api Data type metadata attribute. /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] - internal class DisplayAttribute : Attribute + public class DisplayAttribute : Attribute { /// /// Initializes a new instance of the class. diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index a11622ec1..a768312e6 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -11,7 +11,7 @@ Microsoft Microsoft.OpenApi Microsoft.OpenApi - 1.4.0-preview1 + 1.4.0-preview2 .NET models with JSON and YAML writers for OpenAPI specification © Microsoft Corporation. All rights reserved. OpenAPI .NET diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs index e9701b17c..2dcae12d1 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs @@ -45,10 +45,10 @@ public OpenApiCallback() { } /// public OpenApiCallback(OpenApiCallback callback) { - PathItems = new(callback.PathItems); - UnresolvedReference = callback.UnresolvedReference; - Reference = new(callback.Reference); - Extensions = new Dictionary(callback.Extensions); + PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null; + UnresolvedReference = callback?.UnresolvedReference ?? UnresolvedReference; + Reference = callback?.Reference != null ? new(callback?.Reference) : null; + Extensions = callback?.Extensions != null ? new Dictionary(callback.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs index c23e569c5..1f41080bc 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs @@ -78,16 +78,16 @@ public OpenApiComponents() { } /// public OpenApiComponents(OpenApiComponents components) { - Schemas = new Dictionary(components.Schemas); - Responses = new Dictionary(components.Responses); - Parameters = new Dictionary(components.Parameters); - Examples = new Dictionary(components.Examples); - RequestBodies = new Dictionary(components.RequestBodies); - Headers = new Dictionary(components.Headers); - SecuritySchemes = new Dictionary(components.SecuritySchemes); - Links = new Dictionary(components.Links); - Callbacks = new Dictionary(components.Callbacks); - Extensions = new Dictionary(components.Extensions); + Schemas = components?.Schemas != null ? new Dictionary(components.Schemas) : null; + Responses = components?.Responses != null ? new Dictionary(components.Responses) : null; + Parameters = components?.Parameters != null ? new Dictionary(components.Parameters) : null; + Examples = components?.Examples != null ? new Dictionary(components.Examples) : null; + RequestBodies = components?.RequestBodies != null ? new Dictionary(components.RequestBodies) : null; + Headers = components?.Headers != null ? new Dictionary(components.Headers) : null; + SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary(components.SecuritySchemes) : null; + Links = components?.Links != null ? new Dictionary(components.Links) : null; + Callbacks = components?.Callbacks != null ? new Dictionary(components.Callbacks) : null; + Extensions = components?.Extensions != null ? new Dictionary(components.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiContact.cs b/src/Microsoft.OpenApi/Models/OpenApiContact.cs index 9447d424e..352697bf2 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiContact.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiContact.cs @@ -45,10 +45,10 @@ public OpenApiContact() { } /// public OpenApiContact(OpenApiContact contact) { - Name = contact.Name; - Url = new Uri(contact.Url.OriginalString); - Email = contact.Email; - Extensions = new Dictionary(contact.Extensions); + Name = contact?.Name ?? Name; + Url = contact?.Url != null ? new Uri(contact.Url.OriginalString) : null; + Email = contact?.Email ?? Email; + Extensions = contact?.Extensions != null ? new Dictionary(contact.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs b/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs index e03c7d59a..9ae7f0e6a 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs @@ -32,8 +32,8 @@ public OpenApiDiscriminator() { } /// public OpenApiDiscriminator(OpenApiDiscriminator discriminator) { - PropertyName = discriminator.PropertyName; - Mapping = new Dictionary(discriminator.Mapping); + PropertyName = discriminator?.PropertyName ?? PropertyName; + Mapping = discriminator?.Mapping != null ? new Dictionary(discriminator.Mapping) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index 44cbc71ab..01edcebba 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -72,15 +72,15 @@ public OpenApiDocument() {} /// public OpenApiDocument(OpenApiDocument document) { - Workspace = new(document.Workspace); - Info = new(document.Info); - Servers = new List(document.Servers); - Paths = new(document.Paths); - Components = new(document.Components); - SecurityRequirements = new List(document.SecurityRequirements); - Tags = new List(document.Tags); - ExternalDocs = new(document.ExternalDocs); - Extensions = new Dictionary(document.Extensions); + Workspace = document?.Workspace != null ? new(document?.Workspace) : null; + Info = document?.Info != null ? new(document?.Info) : null; + Servers = document?.Servers != null ? new List(document.Servers) : null; + Paths = document?.Paths != null ? new(document?.Paths) : null; + Components = document?.Components != null ? new(document?.Components) : null; + SecurityRequirements = document?.SecurityRequirements != null ? new List(document.SecurityRequirements) : null; + Tags = document?.Tags != null ? new List(document.Tags) : null; + ExternalDocs = document?.ExternalDocs != null ? new(document?.ExternalDocs) : null; + Extensions = document?.Extensions != null ? new Dictionary(document.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs b/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs index 533cb7e80..ddb4162bc 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs @@ -63,12 +63,12 @@ public OpenApiEncoding() {} /// public OpenApiEncoding(OpenApiEncoding encoding) { - ContentType = encoding.ContentType; - Headers = new Dictionary(encoding.Headers); - Style = encoding.Style; - Explode = encoding.Explode; - AllowReserved = encoding.AllowReserved; - Extensions = new Dictionary(encoding.Extensions); + ContentType = encoding?.ContentType ?? ContentType; + Headers = encoding?.Headers != null ? new Dictionary(encoding.Headers) : null; + Style = encoding?.Style ?? Style; + Explode = encoding?.Explode ?? Explode; + AllowReserved = encoding?.AllowReserved ?? AllowReserved; + Extensions = encoding?.Extensions != null ? new Dictionary(encoding.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiExample.cs b/src/Microsoft.OpenApi/Models/OpenApiExample.cs index 5e105be26..4d091a361 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExample.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExample.cs @@ -64,13 +64,13 @@ public OpenApiExample() {} /// public OpenApiExample(OpenApiExample example) { - Summary = example.Summary; - Description = example.Description; - Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor(example.Value); - ExternalValue = example.ExternalValue; - Extensions = new Dictionary(example.Extensions); - Reference = new(example.Reference); - UnresolvedReference = example.UnresolvedReference; + Summary = example?.Summary ?? Summary; + Description = example?.Description ?? Description; + Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor(example?.Value); + ExternalValue = example?.ExternalValue ?? ExternalValue; + Extensions = example?.Extensions != null ? new Dictionary(example.Extensions) : null; + Reference = example?.Reference != null ? new(example?.Reference) : null; + UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs b/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs index 95af8f01b..9ad3b9e55 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs @@ -39,9 +39,9 @@ public OpenApiExternalDocs() {} /// public OpenApiExternalDocs(OpenApiExternalDocs externalDocs) { - Description = externalDocs.Description; - Url = new Uri(externalDocs.Url.OriginalString); - Extensions = new Dictionary(externalDocs.Extensions); + Description = externalDocs?.Description ?? Description; + Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString) : null; + Extensions = externalDocs?.Extensions != null ? new Dictionary(externalDocs.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs index b91440df8..fb4411478 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs @@ -96,20 +96,20 @@ public OpenApiHeader() {} /// public OpenApiHeader(OpenApiHeader header) { - UnresolvedReference = header.UnresolvedReference; - Reference = new(header.Reference); - Description = header.Description; - Required = header.Required; - Deprecated = header.Deprecated; - AllowEmptyValue = header.AllowEmptyValue; - Style = header.Style; - Explode = header.Explode; - AllowReserved = header.AllowReserved; - Schema = new(header.Schema); - Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header.Example); - Examples = new Dictionary(header.Examples); - Content = new Dictionary(header.Content); - Extensions = new Dictionary(header.Extensions); + UnresolvedReference = header?.UnresolvedReference ?? UnresolvedReference; + Reference = header?.Reference != null ? new(header?.Reference) : null; + Description = header?.Description ?? Description; + Required = header?.Required ?? Required; + Deprecated = header?.Deprecated ?? Deprecated; + AllowEmptyValue = header?.AllowEmptyValue ?? AllowEmptyValue; + Style = header?.Style ?? Style; + Explode = header?.Explode ?? Explode; + AllowReserved = header?.AllowReserved ?? AllowReserved; + Schema = header?.Schema != null ? new(header?.Schema) : null; + Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header?.Example); + Examples = header?.Examples != null ? new Dictionary(header.Examples) : null; + Content = header?.Content != null ? new Dictionary(header.Content) : null; + Extensions = header?.Extensions != null ? new Dictionary(header.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiInfo.cs b/src/Microsoft.OpenApi/Models/OpenApiInfo.cs index c5a44c448..df0aa0a49 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiInfo.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiInfo.cs @@ -59,13 +59,13 @@ public OpenApiInfo() {} /// public OpenApiInfo(OpenApiInfo info) { - Title = info.Title; - Description = info.Description; - Version = info.Version; - TermsOfService = info.TermsOfService; - Contact = new(info.Contact); - License = new(info.License); - Extensions = new Dictionary(info.Extensions); + Title = info?.Title ?? Title; + Description = info?.Description ?? Description; + Version = info?.Version ?? Version; + TermsOfService = info?.TermsOfService ?? TermsOfService; + Contact = info?.Contact != null ? new(info?.Contact) : null; + License = info?.License != null ? new(info?.License) : null; + Extensions = info?.Extensions != null ? new Dictionary(info.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiLicense.cs b/src/Microsoft.OpenApi/Models/OpenApiLicense.cs index 431789aac..1a8d1a4d8 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiLicense.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiLicense.cs @@ -39,9 +39,9 @@ public OpenApiLicense() {} /// public OpenApiLicense(OpenApiLicense license) { - Name = license.Name; - Url = new Uri(license.Url.OriginalString); - Extensions = new Dictionary(license.Extensions); + Name = license?.Name ?? Name; + Url = license?.Url != null ? new Uri(license.Url.OriginalString) : null; + Extensions = license?.Extensions != null ? new Dictionary(license.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiLink.cs b/src/Microsoft.OpenApi/Models/OpenApiLink.cs index 6ba3a65fd..b682744e9 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiLink.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiLink.cs @@ -71,15 +71,15 @@ public OpenApiLink() {} /// public OpenApiLink(OpenApiLink link) { - OperationRef = link.OperationRef; - OperationId = link.OperationId; - Parameters = new(link.Parameters); - RequestBody = new(link.RequestBody); - Description = link.Description; - Server = new(link.Server); - Extensions = new Dictionary(link.Extensions); - UnresolvedReference = link.UnresolvedReference; - Reference = new(link.Reference); + OperationRef = link?.OperationRef ?? OperationRef; + OperationId = link?.OperationId ?? OperationId; + Parameters = link?.Parameters != null ? new(link?.Parameters) : null; + RequestBody = link?.RequestBody != null ? new(link?.RequestBody) : null; + Description = link?.Description ?? Description; + Server = link?.Server != null ? new(link?.Server) : null; + Extensions = link?.Extensions != null ? new Dictionary(link.Extensions) : null; + UnresolvedReference = link?.UnresolvedReference ?? UnresolvedReference; + Reference = link?.Reference != null ? new(link?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs index 94dcbdfa7..63a58cd02 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs @@ -53,11 +53,11 @@ public OpenApiMediaType() {} /// public OpenApiMediaType(OpenApiMediaType mediaType) { - Schema = new(mediaType.Schema); - Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(mediaType.Example); - Examples = new Dictionary(mediaType.Examples); - Encoding = new Dictionary(mediaType.Encoding); - Extensions = new Dictionary(mediaType.Extensions); + Schema = mediaType?.Schema != null ? new(mediaType?.Schema) : null; + Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(mediaType?.Example); + Examples = mediaType?.Examples != null ? new Dictionary(mediaType.Examples) : null; + Encoding = mediaType?.Encoding != null ? new Dictionary(mediaType.Encoding) : null; + Extensions = mediaType?.Extensions != null ? new Dictionary(mediaType.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs index 02856d4cd..c6f91fbd8 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs @@ -51,11 +51,11 @@ public OpenApiOAuthFlow() {} /// public OpenApiOAuthFlow(OpenApiOAuthFlow oAuthFlow) { - AuthorizationUrl = new Uri(oAuthFlow.AuthorizationUrl.OriginalString); - TokenUrl = new Uri(oAuthFlow.TokenUrl.OriginalString); - RefreshUrl = new Uri(oAuthFlow.RefreshUrl.OriginalString); - Scopes = new Dictionary(oAuthFlow.Scopes); - Extensions = new Dictionary(oAuthFlow.Extensions); + AuthorizationUrl = oAuthFlow?.AuthorizationUrl != null ? new Uri(oAuthFlow.AuthorizationUrl.OriginalString) : null; + TokenUrl = oAuthFlow?.TokenUrl != null ? new Uri(oAuthFlow.TokenUrl.OriginalString) : null; + RefreshUrl = oAuthFlow?.RefreshUrl != null ? new Uri(oAuthFlow.RefreshUrl.OriginalString) : null; + Scopes = oAuthFlow?.Scopes != null ? new Dictionary(oAuthFlow.Scopes) : null; + Extensions = oAuthFlow?.Extensions != null ? new Dictionary(oAuthFlow.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs index 973a403e0..8443e6730 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs @@ -49,11 +49,11 @@ public OpenApiOAuthFlows() {} /// public OpenApiOAuthFlows(OpenApiOAuthFlows oAuthFlows) { - Implicit = new(oAuthFlows.Implicit); - Password = new(oAuthFlows.Password); - ClientCredentials = new(oAuthFlows.ClientCredentials); - AuthorizationCode = new(oAuthFlows.AuthorizationCode); - Extensions = new Dictionary(oAuthFlows.Extensions); + Implicit = oAuthFlows?.Implicit != null ? new(oAuthFlows?.Implicit) : null; + Password = oAuthFlows?.Password != null ? new(oAuthFlows?.Password) : null; + ClientCredentials = oAuthFlows?.ClientCredentials != null ? new(oAuthFlows?.ClientCredentials) : null; + AuthorizationCode = oAuthFlows?.AuthorizationCode != null ? new(oAuthFlows?.AuthorizationCode) : null; + Extensions = oAuthFlows?.Extensions != null ? new Dictionary(oAuthFlows.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs index 775532684..ba0af7317 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs @@ -116,19 +116,19 @@ public OpenApiOperation() {} /// public OpenApiOperation(OpenApiOperation operation) { - Tags = new List(operation.Tags); - Summary = operation.Summary; - Description = operation.Description; - ExternalDocs = new(operation.ExternalDocs); - OperationId = operation.OperationId; - Parameters = new List(operation.Parameters); - RequestBody = new(operation.RequestBody); - Responses = new(operation.Responses); - Callbacks = new Dictionary(operation.Callbacks); - Deprecated = operation.Deprecated; - Security = new List(operation.Security); - Servers = new List(operation.Servers); - Extensions = new Dictionary(operation.Extensions); + Tags = new List(operation?.Tags); + Summary = operation?.Summary ?? Summary; + Description = operation?.Description ?? Description; + ExternalDocs = operation?.ExternalDocs != null ? new(operation?.ExternalDocs) : null; + OperationId = operation?.OperationId ?? OperationId; + Parameters = operation?.Parameters != null ? new List(operation.Parameters) : null; + RequestBody = new(operation?.RequestBody); + Responses = operation?.Responses != null ? new(operation?.Responses) : null; + Callbacks = operation?.Callbacks != null ? new Dictionary(operation.Callbacks) : null; + Deprecated = operation?.Deprecated ?? Deprecated; + Security = operation?.Security != null ? new List(operation.Security) : null; + Servers = operation?.Servers != null ? new List(operation.Servers) : null; + Extensions = operation?.Extensions != null ? new Dictionary(operation.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index f0b21b0d9..c6f06b1f6 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -146,22 +146,22 @@ public OpenApiParameter() {} /// public OpenApiParameter(OpenApiParameter parameter) { - UnresolvedReference = parameter.UnresolvedReference; - Reference = new(parameter.Reference); - Name = parameter.Name; - In = parameter.In; - Description = parameter.Description; - Required = parameter.Required; - Style = parameter.Style; - Explode = parameter.Explode; - AllowReserved = parameter.AllowReserved; - Schema = new(parameter.Schema); - Examples = new Dictionary(parameter.Examples); - Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(parameter.Example); - Content = new Dictionary(parameter.Content); - Extensions = new Dictionary(parameter.Extensions); - AllowEmptyValue = parameter.AllowEmptyValue; - Deprecated = parameter.Deprecated; + UnresolvedReference = parameter?.UnresolvedReference ?? UnresolvedReference; + Reference = parameter?.Reference != null ? new(parameter?.Reference) : null; + Name = parameter?.Name ?? Name; + In = parameter?.In ?? In; + Description = parameter?.Description ?? Description; + Required = parameter?.Required ?? Required; + Style = parameter?.Style ?? Style; + Explode = parameter?.Explode ?? Explode; + AllowReserved = parameter?.AllowReserved ?? AllowReserved; + Schema = parameter?.Schema != null ? new(parameter?.Schema) : null; + Examples = parameter?.Examples != null ? new Dictionary(parameter.Examples) : null; + Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(parameter?.Example); + Content = parameter?.Content != null ? new Dictionary(parameter.Content) : null; + Extensions = parameter?.Extensions != null ? new Dictionary(parameter.Extensions) : null; + AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue; + Deprecated = parameter?.Deprecated ?? Deprecated; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs index 8ce83c9eb..ddd358dc2 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs @@ -75,14 +75,14 @@ public OpenApiPathItem() {} /// public OpenApiPathItem(OpenApiPathItem pathItem) { - Summary = pathItem.Summary; - Description = pathItem.Description; - Operations = new Dictionary(pathItem.Operations); - Servers = new List(pathItem.Servers); - Parameters = new List(pathItem.Parameters); - Extensions = new Dictionary(pathItem.Extensions); - UnresolvedReference = pathItem.UnresolvedReference; - Reference = new(pathItem.Reference); + Summary = pathItem?.Summary ?? Summary; + Description = pathItem?.Description ?? Description; + Operations = pathItem?.Operations != null ? new Dictionary(pathItem.Operations) : null; + Servers = pathItem?.Servers != null ? new List(pathItem.Servers) : null; + Parameters = pathItem?.Parameters != null ? new List(pathItem.Parameters) : null; + Extensions = pathItem?.Extensions != null ? new Dictionary(pathItem.Extensions) : null; + UnresolvedReference = pathItem?.UnresolvedReference ?? UnresolvedReference; + Reference = pathItem?.Reference != null ? new(pathItem?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiReference.cs b/src/Microsoft.OpenApi/Models/OpenApiReference.cs index 9213e77bc..31cc5a6e8 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiReference.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiReference.cs @@ -122,10 +122,10 @@ public OpenApiReference() {} /// public OpenApiReference(OpenApiReference reference) { - ExternalResource = reference.ExternalResource; - Type = reference.Type; - Id = reference.Id; - HostDocument = new(reference.HostDocument); + ExternalResource = reference?.ExternalResource; + Type = reference?.Type; + Id = reference?.Id; + HostDocument = new(reference?.HostDocument); } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index b82b67e8a..9016fd7a3 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs @@ -55,12 +55,12 @@ public OpenApiRequestBody() { } /// public OpenApiRequestBody(OpenApiRequestBody requestBody) { - UnresolvedReference = requestBody.UnresolvedReference; - Reference = new(requestBody.Reference); - Description = requestBody.Description; - Required = requestBody.Required; - Content = new Dictionary(requestBody.Content); - Extensions = new Dictionary(requestBody.Extensions); + UnresolvedReference = requestBody?.UnresolvedReference ?? UnresolvedReference; + Reference = requestBody?.Reference != null ? new(requestBody?.Reference) : null; + Description = requestBody?.Description ?? Description; + Required = requestBody?.Required ?? Required; + Content = requestBody?.Content != null ? new Dictionary(requestBody.Content) : null; + Extensions = requestBody?.Extensions != null ? new Dictionary(requestBody.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiResponse.cs b/src/Microsoft.OpenApi/Models/OpenApiResponse.cs index cf0c796e6..a173f6c1a 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiResponse.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiResponse.cs @@ -61,13 +61,13 @@ public OpenApiResponse() {} /// public OpenApiResponse(OpenApiResponse response) { - Description = response.Description; - Headers = new Dictionary(response.Headers); - Content = new Dictionary(response.Content); - Links = new Dictionary(response.Links); - Extensions = new Dictionary(response.Extensions); - UnresolvedReference = response.UnresolvedReference; - Reference = new(response.Reference); + Description = response?.Description ?? Description; + Headers = response?.Headers != null ? new Dictionary(response.Headers) : null; + Content = response?.Content != null ? new Dictionary(response.Content) : null; + Links = response?.Links != null ? new Dictionary(response.Links) : null; + Extensions = response?.Extensions != null ? new Dictionary(response.Extensions) : null; + UnresolvedReference = response?.UnresolvedReference ?? UnresolvedReference; + Reference = response?.Reference != null ? new(response?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index d43756887..3886a5555 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -252,44 +252,44 @@ public OpenApiSchema() {} /// public OpenApiSchema(OpenApiSchema schema) { - Title = schema.Title; - Type = schema.Type; - Format = schema.Format; - Description = schema.Description; - Maximum = schema.Maximum; - ExclusiveMaximum = schema.ExclusiveMaximum; - Minimum = schema.Minimum; - ExclusiveMinimum = schema.ExclusiveMinimum; - MaxLength = schema.MaxLength; - MinLength = schema.MinLength; - Pattern = schema.Pattern; - MultipleOf = schema.MultipleOf; - Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema.Default); - ReadOnly = schema.ReadOnly; - WriteOnly = schema.WriteOnly; - AllOf = new List(schema.AllOf); - OneOf = new List(schema.OneOf); - AnyOf = new List(schema.AnyOf); - Not = new(schema.Not); - Required = new HashSet(schema.Required); - Items = new(schema.Items); - MaxItems = schema.MaxItems; - MinItems = schema.MinItems; - UniqueItems = schema.UniqueItems; - Properties = new Dictionary(schema.Properties); - MaxProperties = schema.MaxProperties; - MinProperties = schema.MinProperties; - AdditionalPropertiesAllowed = schema.AdditionalPropertiesAllowed; - AdditionalProperties = new(schema.AdditionalProperties); - Discriminator = new(schema.Discriminator); - Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema.Example); - Enum = new List(schema.Enum); - Nullable = schema.Nullable; - ExternalDocs = new(schema.ExternalDocs); - Deprecated = schema.Deprecated; - Xml = new(schema.Xml); - UnresolvedReference = schema.UnresolvedReference; - Reference = new(schema.Reference); + Title = schema?.Title ?? Title; + Type = schema?.Type ?? Type; + Format = schema?.Format ?? Format; + Description = schema?.Description ?? Description; + Maximum = schema?.Maximum ?? Maximum; + ExclusiveMaximum = schema?.ExclusiveMaximum ?? ExclusiveMaximum; + Minimum = schema?.Minimum ?? Minimum; + ExclusiveMinimum = schema?.ExclusiveMinimum ?? ExclusiveMinimum; + MaxLength = schema?.MaxLength ?? MaxLength; + MinLength = schema?.MinLength ?? MinLength; + Pattern = schema?.Pattern ?? Pattern; + MultipleOf = schema?.MultipleOf ?? MultipleOf; + Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema?.Default); + ReadOnly = schema?.ReadOnly ?? ReadOnly; + WriteOnly = schema?.WriteOnly ?? WriteOnly; + AllOf = schema?.AllOf != null ? new List(schema.AllOf) : null; + OneOf = schema?.OneOf != null ? new List(schema.OneOf) : null; + AnyOf = schema?.AnyOf != null ? new List(schema.AnyOf) : null; + Not = schema?.Not != null ? new(schema?.Not) : null; + Required = schema?.Required != null ? new HashSet(schema.Required) : null; + Items = schema?.Items != null ? new(schema?.Items) : null; + MaxItems = schema?.MaxItems ?? MaxItems; + MinItems = schema?.MinItems ?? MinItems; + UniqueItems = schema?.UniqueItems ?? UniqueItems; + Properties = schema?.Properties != null ? new Dictionary(schema.Properties) : null; + MaxProperties = schema?.MaxProperties ?? MaxProperties; + MinProperties = schema?.MinProperties ?? MinProperties; + AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed; + AdditionalProperties = new(schema?.AdditionalProperties); + Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null; + Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(schema?.Example); + Enum = schema?.Enum != null ? new List(schema.Enum) : null; + Nullable = schema?.Nullable ?? Nullable; + ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null; + Deprecated = schema?.Deprecated ?? Deprecated; + Xml = schema?.Xml != null ? new(schema?.Xml) : null; + UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference; + Reference = schema?.Reference != null ? new(schema?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs index b87adf573..913e70441 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs @@ -84,17 +84,17 @@ public OpenApiSecurityScheme() {} /// public OpenApiSecurityScheme(OpenApiSecurityScheme securityScheme) { - Type = securityScheme.Type; - Description = securityScheme.Description; - Name = securityScheme.Name; - In = securityScheme.In; - Scheme = securityScheme.Scheme; - BearerFormat = securityScheme.BearerFormat; - Flows = new(securityScheme.Flows); - OpenIdConnectUrl = new Uri(securityScheme.OpenIdConnectUrl.OriginalString); - Extensions = new Dictionary(securityScheme.Extensions); - UnresolvedReference = securityScheme.UnresolvedReference; - Reference = new(securityScheme.Reference); + Type = securityScheme?.Type ?? Type; + Description = securityScheme?.Description ?? Description; + Name = securityScheme?.Name ?? Name; + In = securityScheme?.In ?? In; + Scheme = securityScheme?.Scheme ?? Scheme; + BearerFormat = securityScheme?.BearerFormat ?? BearerFormat; + Flows = securityScheme?.Flows != null ? new(securityScheme?.Flows) : null; + OpenIdConnectUrl = securityScheme?.OpenIdConnectUrl != null ? new Uri(securityScheme.OpenIdConnectUrl.OriginalString) : null; + Extensions = securityScheme?.Extensions != null ? new Dictionary(securityScheme.Extensions) : null; + UnresolvedReference = securityScheme?.UnresolvedReference ?? UnresolvedReference; + Reference = securityScheme?.Reference != null ? new(securityScheme?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiServer.cs b/src/Microsoft.OpenApi/Models/OpenApiServer.cs index 875bef5c7..b3b1d1287 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiServer.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiServer.cs @@ -46,10 +46,10 @@ public OpenApiServer() {} /// public OpenApiServer(OpenApiServer server) { - Description = server.Description; - Url = server.Url; - Variables = new Dictionary(server.Variables); - Extensions = new Dictionary(server.Extensions); + Description = server?.Description ?? Description; + Url = server?.Url ?? Url; + Variables = server?.Variables != null ? new Dictionary(server.Variables) : null; + Extensions = server?.Extensions != null ? new Dictionary(server.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs index b1f222e83..70164bc59 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs @@ -44,10 +44,10 @@ public OpenApiServerVariable() {} /// public OpenApiServerVariable(OpenApiServerVariable serverVariable) { - Description = serverVariable.Description; - Default = serverVariable.Default; - Enum = new List(serverVariable.Enum); - Extensions = new Dictionary(serverVariable.Extensions); + Description = serverVariable?.Description; + Default = serverVariable?.Default; + Enum = serverVariable?.Enum != null ? new List(serverVariable?.Enum) : serverVariable?.Enum; + Extensions = serverVariable?.Extensions != null ? new Dictionary(serverVariable?.Extensions) : serverVariable?.Extensions; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiTag.cs b/src/Microsoft.OpenApi/Models/OpenApiTag.cs index 5ecfa0363..ba4129142 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiTag.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiTag.cs @@ -53,12 +53,12 @@ public OpenApiTag() {} /// public OpenApiTag(OpenApiTag tag) { - Name = tag.Name; - Description = tag.Description; - ExternalDocs = new(tag.ExternalDocs); - Extensions = new Dictionary(tag.Extensions); - UnresolvedReference = tag.UnresolvedReference; - Reference = new(tag.Reference); + Name = tag?.Name ?? Name; + Description = tag?.Description ?? Description; + ExternalDocs = tag?.ExternalDocs != null ? new(tag?.ExternalDocs) : null; + Extensions = tag?.Extensions != null ? new Dictionary(tag.Extensions) : null; + UnresolvedReference = tag?.UnresolvedReference ?? UnresolvedReference; + Reference = tag?.Reference != null ? new(tag?.Reference) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiXml.cs b/src/Microsoft.OpenApi/Models/OpenApiXml.cs index eb48132ad..c6719d85e 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiXml.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiXml.cs @@ -56,12 +56,12 @@ public OpenApiXml() {} /// public OpenApiXml(OpenApiXml xml) { - Name = xml.Name; - Namespace = xml.Namespace; - Prefix = xml.Prefix; - Attribute = xml.Attribute; - Wrapped = xml.Wrapped; - Extensions = new Dictionary(xml.Extensions); + Name = xml?.Name ?? Name; + Namespace = xml?.Namespace ?? Namespace; + Prefix = xml?.Prefix ?? Prefix; + Attribute = xml?.Attribute ?? Attribute; + Wrapped = xml?.Wrapped ?? Wrapped; + Extensions = xml?.Extensions != null ? new Dictionary(xml.Extensions) : null; } /// diff --git a/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs b/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs index 85c64dd30..1a1f12a18 100644 --- a/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs +++ b/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs @@ -26,8 +26,8 @@ public RuntimeExpressionAnyWrapper() {} /// public RuntimeExpressionAnyWrapper(RuntimeExpressionAnyWrapper runtimeExpressionAnyWrapper) { - Any = OpenApiAnyCloneHelper.CloneFromCopyConstructor(runtimeExpressionAnyWrapper.Any); - Expression = runtimeExpressionAnyWrapper.Expression; + Any = OpenApiAnyCloneHelper.CloneFromCopyConstructor(runtimeExpressionAnyWrapper?.Any); + Expression = runtimeExpressionAnyWrapper?.Expression; } /// diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs index 1e4739374..4d7f11032 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs @@ -393,9 +393,9 @@ private bool IsScopeType(ScopeType type) /// property name protected void VerifyCanWritePropertyName(string name) { - if (string.IsNullOrWhiteSpace(name)) + if (name == null) { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); + throw Error.ArgumentNull(nameof(name)); } if (Scopes.Count == 0) diff --git a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj index bfb749d0f..83e5482a7 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj +++ b/test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj @@ -250,14 +250,14 @@ - + - + all diff --git a/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj b/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj index 6f1763b30..ef9886bb9 100644 --- a/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj +++ b/test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj @@ -8,9 +8,9 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Microsoft.OpenApi.Tests/Attributes/DisplayAttributeTests.cs b/test/Microsoft.OpenApi.Tests/Attributes/DisplayAttributeTests.cs new file mode 100644 index 000000000..26ec04556 --- /dev/null +++ b/test/Microsoft.OpenApi.Tests/Attributes/DisplayAttributeTests.cs @@ -0,0 +1,28 @@ +using Microsoft.OpenApi.Attributes; +using Microsoft.OpenApi.Extensions; +using Xunit; + +namespace Microsoft.OpenApi.Tests.Attributes +{ + + public enum ApiLevel + { + [DisplayAttribute("private")] + Private = 1, + [DisplayAttribute("public")] + Public = 2, + [DisplayAttribute("corporate")] + Corporate = 3 + } + public class DisplayAttributeTests + { + [Theory] + [InlineData(ApiLevel.Private,"private")] + [InlineData(ApiLevel.Public, "public")] + [InlineData(ApiLevel.Corporate, "corporate")] + public void GetDisplayNameExtensionShouldUseDisplayAttribute(ApiLevel apiLevel, string expected) + { + Assert.Equal(expected, apiLevel.GetDisplayName()); + } + } +} diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index 5104ebbe9..022b0e5dc 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -16,12 +16,12 @@ - - + + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index a3c284b0e..0753ebd74 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -129,6 +129,15 @@ namespace Microsoft.OpenApi.Any Password = 10, } } +namespace Microsoft.OpenApi.Attributes +{ + [System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field)] + public class DisplayAttribute : System.Attribute + { + public DisplayAttribute(string name) { } + public string Name { get; } + } +} namespace Microsoft.OpenApi.Exceptions { public class OpenApiException : System.Exception @@ -938,43 +947,72 @@ namespace Microsoft.OpenApi.Models } public enum OperationType { + [Microsoft.OpenApi.Attributes.Display("get")] Get = 0, + [Microsoft.OpenApi.Attributes.Display("put")] Put = 1, + [Microsoft.OpenApi.Attributes.Display("post")] Post = 2, + [Microsoft.OpenApi.Attributes.Display("delete")] Delete = 3, + [Microsoft.OpenApi.Attributes.Display("options")] Options = 4, + [Microsoft.OpenApi.Attributes.Display("head")] Head = 5, + [Microsoft.OpenApi.Attributes.Display("patch")] Patch = 6, + [Microsoft.OpenApi.Attributes.Display("trace")] Trace = 7, } public enum ParameterLocation { + [Microsoft.OpenApi.Attributes.Display("query")] Query = 0, + [Microsoft.OpenApi.Attributes.Display("header")] Header = 1, + [Microsoft.OpenApi.Attributes.Display("path")] Path = 2, + [Microsoft.OpenApi.Attributes.Display("cookie")] Cookie = 3, } public enum ParameterStyle { + [Microsoft.OpenApi.Attributes.Display("matrix")] Matrix = 0, + [Microsoft.OpenApi.Attributes.Display("label")] Label = 1, + [Microsoft.OpenApi.Attributes.Display("form")] Form = 2, + [Microsoft.OpenApi.Attributes.Display("simple")] Simple = 3, + [Microsoft.OpenApi.Attributes.Display("spaceDelimited")] SpaceDelimited = 4, + [Microsoft.OpenApi.Attributes.Display("pipeDelimited")] PipeDelimited = 5, + [Microsoft.OpenApi.Attributes.Display("deepObject")] DeepObject = 6, } public enum ReferenceType { + [Microsoft.OpenApi.Attributes.Display("schemas")] Schema = 0, + [Microsoft.OpenApi.Attributes.Display("responses")] Response = 1, + [Microsoft.OpenApi.Attributes.Display("parameters")] Parameter = 2, + [Microsoft.OpenApi.Attributes.Display("examples")] Example = 3, + [Microsoft.OpenApi.Attributes.Display("requestBodies")] RequestBody = 4, + [Microsoft.OpenApi.Attributes.Display("headers")] Header = 5, + [Microsoft.OpenApi.Attributes.Display("securitySchemes")] SecurityScheme = 6, + [Microsoft.OpenApi.Attributes.Display("links")] Link = 7, + [Microsoft.OpenApi.Attributes.Display("callbacks")] Callback = 8, + [Microsoft.OpenApi.Attributes.Display("tags")] Tag = 9, } public class RuntimeExpressionAnyWrapper : Microsoft.OpenApi.Interfaces.IOpenApiElement @@ -987,9 +1025,13 @@ namespace Microsoft.OpenApi.Models } public enum SecuritySchemeType { + [Microsoft.OpenApi.Attributes.Display("apiKey")] ApiKey = 0, + [Microsoft.OpenApi.Attributes.Display("http")] Http = 1, + [Microsoft.OpenApi.Attributes.Display("oauth2")] OAuth2 = 2, + [Microsoft.OpenApi.Attributes.Display("openIdConnect")] OpenIdConnect = 3, } }