diff --git a/src/Microsoft.OpenApi/Reader/BaseOpenApiVersionService.cs b/src/Microsoft.OpenApi/Reader/BaseOpenApiVersionService.cs new file mode 100644 index 000000000..6b98c66e3 --- /dev/null +++ b/src/Microsoft.OpenApi/Reader/BaseOpenApiVersionService.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.OpenApi.Reader; + +internal abstract class BaseOpenApiVersionService : IOpenApiVersionService +{ + public OpenApiDiagnostic Diagnostic { get; } + + protected BaseOpenApiVersionService(OpenApiDiagnostic diagnostic) + { + Diagnostic = diagnostic; + } + + internal abstract Dictionary> Loaders { get; } + + public abstract OpenApiDocument LoadDocument(RootNode rootNode, Uri location); + + public T? LoadElement(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement + { + if (Loaders.TryGetValue(typeof(T), out var loader) && loader(node, doc) is T result) + { + return result; + } + return default; + } + public virtual string? GetReferenceScalarValues(MapNode mapNode, string scalarValue) + { + if (mapNode.Any(static x => !"$ref".Equals(x.Name, StringComparison.OrdinalIgnoreCase)) && + mapNode + .Where(x => x.Name.Equals(scalarValue)) + .Select(static x => x.Value) + .OfType().FirstOrDefault() is {} valueNode) + { + return valueNode.GetScalarValue(); + } + + return null; + } +} diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs index aca39ed86..9375683b2 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs @@ -10,17 +10,14 @@ namespace Microsoft.OpenApi.Reader.V2 /// /// The version specific implementations for OpenAPI V2.0. /// - internal class OpenApiV2VersionService : IOpenApiVersionService + internal class OpenApiV2VersionService : BaseOpenApiVersionService { - public OpenApiDiagnostic Diagnostic { get; } - /// /// Create Parsing Context /// /// Provide instance for diagnostic object for collecting and accessing information about the parsing. - public OpenApiV2VersionService(OpenApiDiagnostic diagnostic) + public OpenApiV2VersionService(OpenApiDiagnostic diagnostic): base(diagnostic) { - Diagnostic = diagnostic; } private readonly Dictionary> _loaders = new() @@ -44,22 +41,13 @@ public OpenApiV2VersionService(OpenApiDiagnostic diagnostic) [typeof(OpenApiXml)] = OpenApiV2Deserializer.LoadXml }; - public OpenApiDocument LoadDocument(RootNode rootNode, Uri location) + public override OpenApiDocument LoadDocument(RootNode rootNode, Uri location) { return OpenApiV2Deserializer.LoadOpenApi(rootNode, location); } + internal override Dictionary> Loaders => _loaders; - public T? LoadElement(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement - { - if (_loaders.TryGetValue(typeof(T), out var loader) && loader(node, doc) is T result) - { - return result; - } - return default; - } - - /// - public string GetReferenceScalarValues(MapNode mapNode, string scalarValue) + public override string GetReferenceScalarValues(MapNode mapNode, string scalarValue) { throw new InvalidOperationException(); } diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiV3VersionService.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiV3VersionService.cs index 878dc2fbe..a5e24ff61 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiV3VersionService.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiV3VersionService.cs @@ -3,29 +3,24 @@ using System; using System.Collections.Generic; -using System.Linq; namespace Microsoft.OpenApi.Reader.V3 { /// /// The version service for the Open API V3.0. /// - internal class OpenApiV3VersionService : IOpenApiVersionService + internal class OpenApiV3VersionService : BaseOpenApiVersionService { - public OpenApiDiagnostic Diagnostic { get; } - - private static readonly char[] _pathSeparator = new char[] { '/' }; /// /// Create Parsing Context /// /// Provide instance for diagnostic object for collecting and accessing information about the parsing. - public OpenApiV3VersionService(OpenApiDiagnostic diagnostic) + public OpenApiV3VersionService(OpenApiDiagnostic diagnostic):base(diagnostic) { - Diagnostic = diagnostic; } - private readonly Dictionary> _loaders = new() + private readonly Dictionary> _loaders = new() { [typeof(JsonNodeExtension)] = OpenApiV3Deserializer.LoadAny, [typeof(OpenApiCallback)] = OpenApiV3Deserializer.LoadCallback, @@ -59,29 +54,11 @@ public OpenApiV3VersionService(OpenApiDiagnostic diagnostic) [typeof(OpenApiSchemaReference)] = OpenApiV3Deserializer.LoadMapping }; - public OpenApiDocument LoadDocument(RootNode rootNode, Uri location) - { - return OpenApiV3Deserializer.LoadOpenApi(rootNode, location); - } + internal override Dictionary> Loaders => _loaders; - public T LoadElement(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement + public override OpenApiDocument LoadDocument(RootNode rootNode, Uri location) { - return (T)_loaders[typeof(T)](node, doc); + return OpenApiV3Deserializer.LoadOpenApi(rootNode, location); } - - /// - public string? GetReferenceScalarValues(MapNode mapNode, string scalarValue) - { - if (mapNode.Any(static x => !"$ref".Equals(x.Name, StringComparison.OrdinalIgnoreCase)) && - mapNode - .Where(x => x.Name.Equals(scalarValue)) - .Select(static x => x.Value) - .OfType().FirstOrDefault() is {} valueNode) - { - return valueNode.GetScalarValue(); - } - - return null; - } } } diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiV31VersionService.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiV31VersionService.cs index fafda1eb8..548f5de76 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiV31VersionService.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiV31VersionService.cs @@ -3,34 +3,30 @@ using System; using System.Collections.Generic; -using System.Linq; -using Microsoft.OpenApi.Reader.V3; namespace Microsoft.OpenApi.Reader.V31 { /// /// The version service for the Open API V3.1. /// - internal class OpenApiV31VersionService : IOpenApiVersionService + internal class OpenApiV31VersionService : BaseOpenApiVersionService { - public OpenApiDiagnostic Diagnostic { get; } /// /// Create Parsing Context /// /// Provide instance for diagnotic object for collecting and accessing information about the parsing. - public OpenApiV31VersionService(OpenApiDiagnostic diagnostic) + public OpenApiV31VersionService(OpenApiDiagnostic diagnostic):base(diagnostic) { - Diagnostic = diagnostic; } - private readonly Dictionary> _loaders = new Dictionary> + private readonly Dictionary> _loaders = new Dictionary> { [typeof(JsonNodeExtension)] = OpenApiV31Deserializer.LoadAny, [typeof(OpenApiCallback)] = OpenApiV31Deserializer.LoadCallback, [typeof(OpenApiComponents)] = OpenApiV31Deserializer.LoadComponents, [typeof(OpenApiContact)] = OpenApiV31Deserializer.LoadContact, - [typeof(OpenApiDiscriminator)] = OpenApiV3Deserializer.LoadDiscriminator, + [typeof(OpenApiDiscriminator)] = OpenApiV31Deserializer.LoadDiscriminator, [typeof(OpenApiEncoding)] = OpenApiV31Deserializer.LoadEncoding, [typeof(OpenApiExample)] = OpenApiV31Deserializer.LoadExample, [typeof(OpenApiExternalDocs)] = OpenApiV31Deserializer.LoadExternalDocs, @@ -58,28 +54,11 @@ public OpenApiV31VersionService(OpenApiDiagnostic diagnostic) [typeof(OpenApiSchemaReference)] = OpenApiV31Deserializer.LoadMapping }; - public OpenApiDocument LoadDocument(RootNode rootNode, Uri location) + public override OpenApiDocument LoadDocument(RootNode rootNode, Uri location) { return OpenApiV31Deserializer.LoadOpenApi(rootNode, location); } + internal override Dictionary> Loaders => _loaders; - public T LoadElement(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement - { - return (T)_loaders[typeof(T)](node, doc); - } - - /// - public string? GetReferenceScalarValues(MapNode mapNode, string scalarValue) - { - if (mapNode.Any(static x => !"$ref".Equals(x.Name, StringComparison.OrdinalIgnoreCase))) - { - var valueNode = mapNode.Where(x => x.Name.Equals(scalarValue)) - .Select(static x => x.Value).OfType().FirstOrDefault(); - - return valueNode?.GetScalarValue(); - } - - return null; - } } }