Skip to content

Commit

Permalink
feat: Now it generates full sdk by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 19, 2024
1 parent 0da70c3 commit 267714e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 40 deletions.
13 changes: 9 additions & 4 deletions src/libs/OpenApiGenerator.Cli/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ private static async Task HandleAsync(
TargetFramework: "netstandard2.0",
Namespace: @namespace,
ClassName: clientClassName,
GenerateConstructors: false,
GenerateMethods: false,
NamingConvention: default,
JsonSerializerType: default,
UseRequiredKeyword: default,
GenerateConstructors: false,
GenerateMethods: false,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
GenerateModels: true,
ExcludeOperationIds: [],
GenerateModels: false,
ModelStyle: default,
IncludeModels: []
IncludeModels: [],
ExcludeModels: [],
GenerateSdk: true
);

var models = ModelGeneratorMethods.PrepareData((yaml, settings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ public static ImmutableArray<EndPoint> PrepareData(
CancellationToken cancellationToken = default)
{
var (text, settings) = tuple;
if (settings is { GenerateSdk: false, GenerateMethods: false })
{
return ImmutableArray<EndPoint>.Empty;
}

var openApiDocument = text.GetOpenApiDocument(cancellationToken);

var includedOperationIds = new HashSet<string>(settings.IncludeOperationIds);
var excludedOperationIds = new HashSet<string>(settings.ExcludeOperationIds);

return openApiDocument.Paths.SelectMany(path =>
path.Value.Operations
.Where(x =>
settings.GenerateMethods &&
(includedOperationIds.Count == 0 ||
includedOperationIds.Contains(x.Value.GetOperationIdOrCompute(path: path.Key, operationType: x.Key))))
includedOperationIds.Contains(x.Value.GetOperationIdOrCompute(path: path.Key, operationType: x.Key))) &&
!excludedOperationIds.Contains(x.Value.GetOperationIdOrCompute(path: path.Key, operationType: x.Key)))
.Select(operation => EndPoint.FromSchema(operation, settings, path.Key)))
// Constructor
.Concat(settings.GenerateConstructors ? [new EndPoint(
.Concat(settings.GenerateSdk || settings.GenerateConstructors ? [new EndPoint(
Id: "Constructors",
Namespace: settings.Namespace,
ClassName: settings.ClassName,
Expand Down
15 changes: 11 additions & 4 deletions src/libs/OpenApiGenerator.Core/Generators/ModelGeneratorMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@ public static ImmutableArray<ModelData> PrepareData(
CancellationToken cancellationToken = default)
{
var (text, settings) = tuple;
if (settings is { GenerateSdk: false, GenerateModels: false })
{
return ImmutableArray<ModelData>.Empty;
}

var openApiDocument = text.GetOpenApiDocument(cancellationToken);

var includedModels = new HashSet<string>(settings.IncludeModels);
var excludedModels = new HashSet<string>(settings.ExcludeModels);
var referencesOfIncludedModels = includedModels.Count == 0
? []
: new HashSet<string>(openApiDocument.Components.Schemas
.Where(schema =>
includedModels.Count == 0 ||
includedModels.Contains(schema.Key))
(includedModels.Count == 0 ||
includedModels.Contains(schema.Key)) &&
!excludedModels.Contains(schema.Key))
.SelectMany(schema => schema.GetReferences())
.Select(reference => reference.Id));

var components = openApiDocument.Components.Schemas
.Where(schema =>
includedModels.Count == 0 ||
(includedModels.Count == 0 ||
includedModels.Contains(schema.Key) ||
referencesOfIncludedModels.Contains(schema.Key))
referencesOfIncludedModels.Contains(schema.Key)) &&
!excludedModels.Contains(schema.Key))
.Select(schema => ModelData.FromSchema(schema, settings))
.SelectMany(model => model.WithAdditionalModels())
.Select(model => model with
Expand Down
8 changes: 7 additions & 1 deletion src/libs/OpenApiGenerator.Core/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ public readonly record struct Settings(

bool GenerateConstructors,
bool GenerateMethods,
bool GenerateMethodsAsHttpClientExtensions,
bool GenerateMethodsUsingSystemNetHttpJson,
ImmutableArray<string> IncludeOperationIds,
ImmutableArray<string> ExcludeOperationIds,

bool GenerateModels,
ModelStyle ModelStyle,
ImmutableArray<string> IncludeModels);
ImmutableArray<string> IncludeModels,
ImmutableArray<string> ExcludeModels,

bool GenerateSdk);
11 changes: 9 additions & 2 deletions src/libs/OpenApiGenerator/OpenApiGenerator.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<!-- InSupportedTargetFrameworks/Never/Always. Default: InSupportedTargetFrameworks -->
<CompilerVisibleProperty Include="OpenApiGenerator_UseRequiredKeyword"/>

<!-- Methods generation -->
<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateConstructors"/>
<!-- true/false. Default: false -->
Expand All @@ -35,15 +36,21 @@
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateMethodsUsingSystemNetHttpJson"/>
<!-- List of ids separated by ;. Default: Empty(all models) -->
<CompilerVisibleProperty Include="OpenApiGenerator_IncludeOperationIds"/>

<!-- List of ids separated by ;. Default: Empty -->
<CompilerVisibleProperty Include="OpenApiGenerator_ExcludeOperationIds"/>

<!-- Models generation -->
<!-- true/false. Default: false -->
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateModels"/>
<!-- Class/Record/ReadonlyRecordStruct. Default: Class -->
<CompilerVisibleProperty Include="OpenApiGenerator_ModelStyle"/>
<!-- List of ids separated by ;. Default: Empty(all models) -->
<CompilerVisibleProperty Include="OpenApiGenerator_IncludeModels"/>
<!-- List of ids separated by ;. Default: Empty -->
<CompilerVisibleProperty Include="OpenApiGenerator_ExcludeModels"/>

<!-- true/false. Default: false -->
<!-- Generates full sdk(all methods/models) -->
<!-- true/false. Default: true -->
<CompilerVisibleProperty Include="OpenApiGenerator_GenerateSdk"/>
</ItemGroup>

Expand Down
16 changes: 12 additions & 4 deletions src/libs/OpenApiGenerator/OptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,36 @@ public static Settings GetSettings(

GenerateConstructors: options.GetBoolGlobalOption(nameof(Settings.GenerateConstructors), prefix),
GenerateMethods: options.GetBoolGlobalOption(nameof(Settings.GenerateMethods), prefix),
GenerateMethodsAsHttpClientExtensions: options.GetBoolGlobalOption(nameof(Settings.GenerateMethodsAsHttpClientExtensions), prefix),
GenerateMethodsUsingSystemNetHttpJson: options.GetBoolGlobalOption(nameof(Settings.GenerateMethodsUsingSystemNetHttpJson), prefix),
IncludeOperationIds: (options.GetGlobalOption(nameof(Settings.IncludeOperationIds), prefix)?.Split(';') ??
[]).ToImmutableArray(),
ExcludeOperationIds: (options.GetGlobalOption(nameof(Settings.ExcludeOperationIds), prefix)?.Split(';') ??
[]).ToImmutableArray(),

GenerateModels: options.GetBoolGlobalOption(nameof(Settings.GenerateModels), prefix),
ModelStyle: options.GetEnumGlobalOption<ModelStyle>(nameof(Settings.ModelStyle), prefix),
IncludeModels: (options.GetGlobalOption(nameof(Settings.IncludeModels), prefix)?.Split(';') ??
[]).ToImmutableArray()

[]).ToImmutableArray(),
ExcludeModels: (options.GetGlobalOption(nameof(Settings.ExcludeModels), prefix)?.Split(';') ??
[]).ToImmutableArray(),

GenerateSdk: options.GetBoolGlobalOption(nameof(Settings.GenerateSdk), prefix, defaultValue: true)
);
}

public static bool GetBoolGlobalOption(
this AnalyzerConfigOptionsProvider provider,
string name,
string? prefix = null)
string? prefix = null,
bool defaultValue = false)
{
provider = provider ?? throw new ArgumentNullException(nameof(provider));
name = name ?? throw new ArgumentNullException(nameof(name));

return bool.TryParse(
provider.GetGlobalOption(name, prefix),
out var value) && value;
out var value) ? value : defaultValue;
}

public static T GetEnumGlobalOption<T>(
Expand Down
28 changes: 14 additions & 14 deletions src/tests/OpenApiGenerator.SnapshotTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public Task Empty(JsonSerializerType jsonSerializerType)
{
return CheckSourceAsync<ModelGenerator>(jsonSerializerType, [], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
}, additionalGenerators: [new ClientGenerator()]);
}

Expand All @@ -64,8 +64,8 @@ public Task LangSmith(JsonSerializerType jsonSerializerType)
text: H.Resources.langsmith_yaml.AsString())
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
}, additionalGenerators: [new ClientGenerator()]);
}

Expand All @@ -80,8 +80,8 @@ public Task Ollama(JsonSerializerType jsonSerializerType)
text: H.Resources.ollamacurated_yaml.AsString())
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_IncludeOperationIds"] = "ListModels",
//["build_property.OpenApiGenerator_IncludeModels"] = "CreateModerationResponse;Error;ErrorResponse;ListModelsResponse;Model;DeleteModelResponse;CreateCompletionRequest",
}, additionalGenerators: [new ClientGenerator()]);
Expand All @@ -98,8 +98,8 @@ public Task OpenAi(JsonSerializerType jsonSerializerType)
text: H.Resources.openai_yaml.AsString())
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_IncludeOperationIds"] = "ListModels",
//["build_property.OpenApiGenerator_IncludeModels"] = "CreateEmbeddingRequest;CreateModerationResponse;Error;ErrorResponse;ListModelsResponse;Model;DeleteModelResponse;CreateCompletionRequest",
}, additionalGenerators: [new ClientGenerator()]);
Expand All @@ -116,8 +116,8 @@ public Task YamlWithLocalFile(JsonSerializerType jsonSerializerType)
text: H.Resources.ipinfo_yaml.AsString()),
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
}, additionalGenerators: [new ClientGenerator()]);
}

Expand All @@ -132,8 +132,8 @@ public Task OpenApi3_0_Yaml(JsonSerializerType jsonSerializerType)
new CustomAdditionalText("openapi.yaml", yaml),
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
}, additionalGenerators: [new ClientGenerator()]);
}

Expand All @@ -146,8 +146,8 @@ public Task YamlWithUrl(JsonSerializerType jsonSerializerType)
new CustomAdditionalText("https://dedoose-rest-api.onrender.com/swagger/v1/swagger.json", string.Empty),
], new Dictionary<string, string>
{
["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
["build_property.OpenApiGenerator_GenerateMethods"] = "true",
//["build_property.OpenApiGenerator_GenerateConstructors"] = "true",
//["build_property.OpenApiGenerator_GenerateMethods"] = "true",
}, additionalGenerators: [new ClientGenerator()]);
}
}
11 changes: 8 additions & 3 deletions src/tests/OpenApiGenerator.UnitTests/CacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ public class CacheTests
NamingConvention: default,
JsonSerializerType: default,
UseRequiredKeyword: default,
GenerateConstructors: true,
GenerateMethods: true,
GenerateConstructors: false,
GenerateMethods: false,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: ImmutableArray.Create(["123", "456"]),
ExcludeOperationIds: [],
GenerateModels: false,
ModelStyle: default,
IncludeModels: ImmutableArray.Create(["123", "456"])
IncludeModels: ImmutableArray.Create(["123", "456"]),
ExcludeModels: [],
GenerateSdk: true
);

// [TestMethod]
Expand Down
11 changes: 8 additions & 3 deletions src/tests/OpenApiGenerator.UnitTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ public class ClientTests :
NamingConvention: default,
JsonSerializerType: default,
UseRequiredKeyword: default,
GenerateConstructors: true,
GenerateMethods: true,
GenerateConstructors: false,
GenerateMethods: false,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
ExcludeOperationIds: [],
GenerateModels: false,
ModelStyle: default,
IncludeModels: []
IncludeModels: [],
ExcludeModels: [],
GenerateSdk: true
);

private Task VerifyAsync(
Expand Down
9 changes: 7 additions & 2 deletions src/tests/OpenApiGenerator.UnitTests/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public class ModelTests :
UseRequiredKeyword: default,
GenerateConstructors: false,
GenerateMethods: false,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
GenerateModels: true,
ExcludeOperationIds: [],
GenerateModels: false,
ModelStyle: default,
IncludeModels: []
IncludeModels: [],
ExcludeModels: [],
GenerateSdk: true
);

private Task VerifyAsync(
Expand Down

0 comments on commit 267714e

Please sign in to comment.