From ca85e9c98c841b3707f2196e01886bd2cd33e677 Mon Sep 17 00:00:00 2001 From: HavenDV Date: Sat, 15 Jun 2024 20:40:05 +0400 Subject: [PATCH] fix: Fixed some Named AnyOf class names. --- src/libs/Directory.Build.props | 2 +- .../Extensions/OpenApiExtensions.cs | 47 +++++++++++++++---- .../OpenApiGenerator.Core/Generation/Data.cs | 10 ++-- .../OpenApiGenerator.SnapshotTests/Tests.cs | 4 +- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/libs/Directory.Build.props b/src/libs/Directory.Build.props index 7b647317f9..932136eb3a 100644 --- a/src/libs/Directory.Build.props +++ b/src/libs/Directory.Build.props @@ -39,7 +39,7 @@ - 0.10.4 + 0.10.5 0.1 v dev diff --git a/src/libs/OpenApiGenerator.Core/Extensions/OpenApiExtensions.cs b/src/libs/OpenApiGenerator.Core/Extensions/OpenApiExtensions.cs index a9c537237c..e8fb1ffa52 100644 --- a/src/libs/OpenApiGenerator.Core/Extensions/OpenApiExtensions.cs +++ b/src/libs/OpenApiGenerator.Core/Extensions/OpenApiExtensions.cs @@ -1,5 +1,6 @@ using System.Globalization; using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Readers; using OpenApiGenerator.Core.Json; @@ -218,18 +219,11 @@ public static string[] Append( return parents.Concat([schema.Key.ToPropertyName()]).ToArray(); } - public static OpenApiRequestBody ResolveIfRequired(this OpenApiRequestBody body) + public static T ResolveIfRequired(this T referenceable) where T : class, IOpenApiReferenceable { - body = body ?? throw new ArgumentNullException(nameof(body)); + referenceable = referenceable ?? throw new ArgumentNullException(nameof(referenceable)); - return body.Reference?.HostDocument?.ResolveReference(body.Reference) as OpenApiRequestBody ?? body; - } - - public static OpenApiResponse ResolveIfRequired(this OpenApiResponse response) - { - response = response ?? throw new ArgumentNullException(nameof(response)); - - return response.Reference?.HostDocument?.ResolveReference(response.Reference) as OpenApiResponse ?? response; + return referenceable.Reference?.HostDocument?.ResolveReference(referenceable.Reference) as T ?? referenceable; } public static KeyValuePair WithKey( @@ -328,6 +322,37 @@ public static string[] FindAllModelsForTag( .Where(x => x.Value.Tags?.Any(y => y.Name == tag) != false) .ToArray(); + var schemas = operations + .SelectMany(x => x.Value.RequestBody?.ResolveIfRequired().Content.Values ?? []) + .Select(x => x.Schema) + .Concat(operations + .SelectMany(x => x.Value.Parameters) + .Select(x => x.ResolveIfRequired().Schema)) + .Concat(operations + .SelectMany(x => x.Value.Responses.Values) + .SelectMany(x => x.ResolveIfRequired().Content.Values) + .Select(x => x.Schema)) + .Where(x => x != null) + .SelectMany(x => new [] { x, x.Items?.ResolveIfRequired() } + .Concat(x.Properties.Values.Select(y => y.ResolveIfRequired())) + .Concat(x.AnyOf.Select(y => y.ResolveIfRequired())) + .Concat(x.OneOf.Select(y => y.ResolveIfRequired())) + .Concat(x.AllOf.Select(y => y.ResolveIfRequired()))) + .Where(x => x != null) + // .SelectMany(x => new [] { x, x!.Items?.ResolveIfRequired() } + // .Concat(x.Properties.Values.Select(y => y.ResolveIfRequired())) + // .Concat(x.AnyOf.Select(y => y.ResolveIfRequired())) + // .Concat(x.OneOf.Select(y => y.ResolveIfRequired())) + // .Concat(x.AllOf.Select(y => y.ResolveIfRequired()))) + // .Where(x => x != null) + // .SelectMany(x => new [] { x, x!.Items?.ResolveIfRequired() } + // .Concat(x.Properties.Values.Select(y => y.ResolveIfRequired())) + // .Concat(x.AnyOf.Select(y => y.ResolveIfRequired())) + // .Concat(x.OneOf.Select(y => y.ResolveIfRequired())) + // .Concat(x.AllOf.Select(y => y.ResolveIfRequired()))) + // .Where(x => x != null) + .ToArray(); + return operations .Select(x => x.Value.RequestBody?.Reference?.Id) .Concat(operations @@ -344,7 +369,9 @@ public static string[] FindAllModelsForTag( .SelectMany(x => x.Value.Responses.Values) .SelectMany(x => x.Content.Values) .Select(x => x.Schema.Items?.Reference?.Id)) + .Concat(schemas.Select(x => x?.Reference?.Id)) .Where(x => x != null) + .Distinct() .ToArray()!; } } \ No newline at end of file diff --git a/src/libs/OpenApiGenerator.Core/Generation/Data.cs b/src/libs/OpenApiGenerator.Core/Generation/Data.cs index 8d0a602646..7028e94e61 100644 --- a/src/libs/OpenApiGenerator.Core/Generation/Data.cs +++ b/src/libs/OpenApiGenerator.Core/Generation/Data.cs @@ -81,8 +81,8 @@ public static (EquatableArray Models, EquatableArray Method Dictionary classes = includedSchemas .SelectMany(schema => ModelData.FromSchemas(schema.Value, settings, schema.Key)) .SelectMany(model => model.WithAdditionalModels()) - //.GroupBy(x => x.ClassName) - //.Select(x => x.First()) + .GroupBy(x => x.ClassName) + .Select(x => x.First()) .ToDictionary(x => x.ClassName, x => x); var operations = openApiDocument.Paths.SelectMany(path => @@ -231,7 +231,7 @@ .. includedTags.Select(x => PropertyData.Default with settings.JsonSerializerType, isTrimming, settings.Namespace, - schema.Key, + schema.Key.ToClassName(), schema.Value.GetSummary(), schema.Value.AnyOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray()))) .ToImmutableArray() : []; @@ -244,7 +244,7 @@ .. includedTags.Select(x => PropertyData.Default with settings.JsonSerializerType, isTrimming, settings.Namespace, - schema.Key, + schema.Key.ToClassName(), schema.Value.GetSummary(), schema.Value.OneOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray()))) .ToImmutableArray() : []; @@ -257,7 +257,7 @@ .. includedTags.Select(x => PropertyData.Default with settings.JsonSerializerType, isTrimming, settings.Namespace, - schema.Key, + schema.Key.ToClassName(), schema.Value.GetSummary(), schema.Value.AllOf.Select((x, i) => TypeData.FromSchema(x.UseReferenceIdOrKey(schema.Key + $"Variant{i + 1}"), settings)).ToImmutableArray()))) .ToImmutableArray() : []; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs b/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs index 33ba66cc83..c299ad2d0a 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs @@ -130,8 +130,8 @@ public Task OpenAi(JsonSerializerType jsonSerializerType) // { // return CheckSourceAsync(jsonSerializerType, [ // new CustomAdditionalText( - // path: H.Resources.api_github_com_yaml.FileName, - // text: H.Resources.api_github_com_yaml.AsString()) + // path: H.Resources.github_yaml.FileName, + // text: H.Resources.github_yaml.AsString()) // ]); // }