Skip to content

Commit

Permalink
fix: Fixed some Named AnyOf class names.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 15, 2024
1 parent 17375d8 commit ca85e9c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.10.4</Version>
<Version>0.10.5</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
47 changes: 37 additions & 10 deletions src/libs/OpenApiGenerator.Core/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<T>(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<string, OpenApiSchema> WithKey(
Expand Down Expand Up @@ -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
Expand All @@ -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()!;
}
}
10 changes: 5 additions & 5 deletions src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public static (EquatableArray<ModelData> Models, EquatableArray<EndPoint> Method
Dictionary<string, ModelData> 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 =>
Expand Down Expand Up @@ -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() : [];
Expand All @@ -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() : [];
Expand All @@ -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() : [];
Expand Down
4 changes: 2 additions & 2 deletions src/tests/OpenApiGenerator.SnapshotTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public Task OpenAi(JsonSerializerType jsonSerializerType)
// {
// return CheckSourceAsync<SdkGenerator>(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())
// ]);
// }

Expand Down

0 comments on commit ca85e9c

Please sign in to comment.