Upgrade generator dep (#730)

* catch up nswag

* fix changes due to api change

* nustache as well
This commit is contained in:
Boshi Lian
2021-10-20 06:53:59 -07:00
committed by GitHub
parent 73d8e99d2a
commit 43f8922d20
13 changed files with 78 additions and 77 deletions

View File

@@ -8,10 +8,10 @@ namespace KubernetesGenerator
{
public class ApiGenerator
{
public void Generate(SwaggerDocument swagger, string outputDirectory)
public void Generate(OpenApiDocument swagger, string outputDirectory)
{
var data = swagger.Operations
.Where(o => o.Method != SwaggerOperationMethod.Options)
.Where(o => o.Method != OpenApiOperationMethod.Options)
.GroupBy(o => o.Operation.OperationId)
.Select(g =>
{
@@ -50,6 +50,7 @@ namespace KubernetesGenerator
.Select(o =>
{
o.Path = o.Path.TrimStart('/');
o.Method = char.ToUpper(o.Method[0]) + o.Method.Substring(1);
return o;
})
.ToArray();

View File

@@ -11,10 +11,10 @@ namespace KubernetesGenerator
{
private readonly Dictionary<string, string> classNameMap;
private readonly HashSet<string> schemaDefinitionsInMultipleGroups;
private readonly Dictionary<JsonSchema4, string> schemaToNameMapCooked;
private readonly Dictionary<JsonSchema4, string> schemaToNameMapUnprocessed;
private readonly Dictionary<JsonSchema, string> schemaToNameMapCooked;
private readonly Dictionary<JsonSchema, string> schemaToNameMapUnprocessed;
public ClassNameHelper(SwaggerDocument swaggerCooked, SwaggerDocument swaggerUnprocessed)
public ClassNameHelper(OpenApiDocument swaggerCooked, OpenApiDocument swaggerUnprocessed)
{
classNameMap = InitClassNameMap(swaggerCooked);
@@ -28,19 +28,19 @@ namespace KubernetesGenerator
Helpers.Register(nameof(GetClassName), GetClassName);
}
private static Dictionary<JsonSchema4, string> GenerateSchemaToNameMapUnprocessed(
SwaggerDocument swaggerUnprocessed)
private static Dictionary<JsonSchema, string> GenerateSchemaToNameMapUnprocessed(
OpenApiDocument swaggerUnprocessed)
{
return swaggerUnprocessed.Definitions.ToDictionary(x => x.Value, x => x.Key);
}
private static Dictionary<JsonSchema4, string> GenerateSchemaToNameMapCooked(SwaggerDocument swaggerCooked)
private static Dictionary<JsonSchema, string> GenerateSchemaToNameMapCooked(OpenApiDocument swaggerCooked)
{
return swaggerCooked.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase());
}
private static HashSet<string> InitSchemaDefinitionsInMultipleGroups(
Dictionary<JsonSchema4, string> schemaToNameMap)
Dictionary<JsonSchema, string> schemaToNameMap)
{
return schemaToNameMap.Values.Select(x =>
{
@@ -60,7 +60,7 @@ namespace KubernetesGenerator
.ToHashSet();
}
private Dictionary<string, string> InitClassNameMap(SwaggerDocument doc)
private Dictionary<string, string> InitClassNameMap(OpenApiDocument doc)
{
var map = new Dictionary<string, string>();
foreach (var (k, v) in doc.Definitions)
@@ -83,17 +83,17 @@ namespace KubernetesGenerator
public void GetClassName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is SwaggerOperation)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiOperation)
{
context.Write(GetClassName(arguments[0] as SwaggerOperation));
context.Write(GetClassName(arguments[0] as OpenApiOperation));
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetClassNameForSchemaDefinition(arguments[0] as JsonSchema4));
context.Write(GetClassNameForSchemaDefinition(arguments[0] as JsonSchema));
}
}
public string GetClassName(SwaggerOperation operation)
public string GetClassName(OpenApiOperation operation)
{
var groupVersionKind =
(Dictionary<string, object>)operation.ExtensionData["x-kubernetes-group-version-kind"];
@@ -109,7 +109,7 @@ namespace KubernetesGenerator
return classNameMap[$"{group}_{kind}_{version}"];
}
public string GetClassName(JsonSchema4 definition)
public string GetClassName(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0];
@@ -117,7 +117,7 @@ namespace KubernetesGenerator
return GetClassName(groupVersionKind);
}
public string GetClassNameForSchemaDefinition(JsonSchema4 definition)
public string GetClassNameForSchemaDefinition(JsonSchema definition)
{
if (definition.ExtensionData != null &&
definition.ExtensionData.ContainsKey("x-kubernetes-group-version-kind"))
@@ -144,7 +144,7 @@ namespace KubernetesGenerator
return $"{group}{version}{entityName}".ToPascalCase();
}
private static Dictionary<JsonSchema4, string> InitSchemaToNameCooked(SwaggerDocument swaggercooked)
private static Dictionary<JsonSchema, string> InitSchemaToNameCooked(OpenApiDocument swaggercooked)
{
return swaggercooked.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase());
}

View File

@@ -27,13 +27,13 @@ namespace KubernetesGenerator
public void GetInterfaceName(RenderContext context, IList<object> arguments,
IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetInterfaceName(arguments[0] as JsonSchema4));
context.Write(GetInterfaceName(arguments[0] as JsonSchema));
}
}
private string GetInterfaceName(JsonSchema4 definition)
private string GetInterfaceName(JsonSchema definition)
{
var interfaces = new List<string>();
if (definition.Properties.TryGetValue("metadata", out var metadataProperty))
@@ -71,7 +71,7 @@ namespace KubernetesGenerator
public void GetMethodName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is SwaggerOperation)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiOperation)
{
string suffix = null;
if (arguments.Count > 1)
@@ -79,16 +79,16 @@ namespace KubernetesGenerator
suffix = arguments[1] as string;
}
context.Write(GetMethodName(arguments[0] as SwaggerOperation, suffix));
context.Write(GetMethodName(arguments[0] as OpenApiOperation, suffix));
}
}
public void GetDotNetName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is SwaggerParameter)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiParameter)
{
var parameter = arguments[0] as SwaggerParameter;
var parameter = arguments[0] as OpenApiParameter;
context.Write(GetDotNetName(parameter.Name));
if (arguments.Count > 1 && arguments[1] as string == "true" && !parameter.IsRequired)
@@ -133,7 +133,7 @@ namespace KubernetesGenerator
{
return "continueProperty";
}
else if (jsonName == "__referencePath")
else if (jsonName == "$ref")
{
return "refProperty";
}
@@ -179,7 +179,7 @@ namespace KubernetesGenerator
return jsonName.ToCamelCase();
}
public static string GetMethodName(SwaggerOperation watchOperation, string suffix)
public static string GetMethodName(OpenApiOperation watchOperation, string suffix)
{
var tag = watchOperation.Tags[0];
tag = tag.Replace("_", string.Empty);

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@@ -6,11 +6,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.3.0"/>
<PackageReference Include="CaseExtensions" Version="1.1.0"/>
<PackageReference Include="CommandLineParser" Version="2.8.0"/>
<PackageReference Include="NSwag.Core" Version="11.17.21"/>
<PackageReference Include="Nustache" Version="1.16.0.8" NoWarn="NU1701"/>
<PackageReference Include="Autofac" Version="6.3.0" />
<PackageReference Include="CaseExtensions" Version="1.1.0" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="NSwag.Core" Version="13.13.2" />
<PackageReference Include="Nustache" Version="1.16.0.10" NoWarn="NU1701" />
</ItemGroup>
<ItemGroup>

View File

@@ -19,13 +19,13 @@ namespace KubernetesGenerator
public static void GetKind(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetKind(arguments[0] as JsonSchema4));
context.Write(GetKind(arguments[0] as JsonSchema));
}
}
private static string GetKind(JsonSchema4 definition)
private static string GetKind(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0];
@@ -36,13 +36,13 @@ namespace KubernetesGenerator
public static void GetGroup(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetGroup(arguments[0] as JsonSchema4));
context.Write(GetGroup(arguments[0] as JsonSchema));
}
}
private static string GetGroup(JsonSchema4 definition)
private static string GetGroup(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0];
@@ -54,13 +54,13 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetApiVersion(arguments[0] as JsonSchema4));
context.Write(GetApiVersion(arguments[0] as JsonSchema));
}
}
private static string GetApiVersion(JsonSchema4 definition)
private static string GetApiVersion(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0];
@@ -72,14 +72,14 @@ namespace KubernetesGenerator
IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null &&
arguments[0] is SwaggerOperationDescription)
arguments[0] is OpenApiOperationDescription)
{
var operation = arguments[0] as SwaggerOperationDescription;
var operation = arguments[0] as OpenApiOperationDescription;
context.Write(GetPathExpression(operation));
}
}
private static string GetPathExpression(SwaggerOperationDescription operation)
private static string GetPathExpression(OpenApiOperationDescription operation)
{
var pathExpression = operation.Path;

View File

@@ -15,7 +15,7 @@ namespace KubernetesGenerator
this.classNameHelper = classNameHelper;
}
public void Generate(SwaggerDocument swagger, string outputDirectory)
public void Generate(OpenApiDocument swagger, string outputDirectory)
{
// Generate the interface declarations
var skippedTypes = new HashSet<string> { "V1WatchEvent" };

View File

@@ -13,7 +13,7 @@ namespace KubernetesGenerator
this.classNameHelper = classNameHelper;
}
public void Generate(SwaggerDocument swaggercooked, string outputDirectory)
public void Generate(OpenApiDocument swaggercooked, string outputDirectory)
{
Directory.CreateDirectory(Path.Combine(outputDirectory, "Models"));

View File

@@ -27,7 +27,7 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var operation = arguments?.FirstOrDefault() as SwaggerOperation;
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string name = null;
@@ -59,7 +59,7 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var schema = arguments[0] as JsonSchema4;
var schema = arguments[0] as JsonSchema;
if (schema != null)
{

View File

@@ -12,7 +12,7 @@ namespace KubernetesGenerator
private readonly Dictionary<string, string> _classNameToPluralMap;
private readonly ClassNameHelper classNameHelper;
public PluralHelper(ClassNameHelper classNameHelper, SwaggerDocument swagger)
public PluralHelper(ClassNameHelper classNameHelper, OpenApiDocument swagger)
{
this.classNameHelper = classNameHelper;
_classNameToPluralMap = InitClassNameToPluralMap(swagger);
@@ -26,9 +26,9 @@ namespace KubernetesGenerator
public void GetPlural(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
var plural = GetPlural(arguments[0] as JsonSchema4);
var plural = GetPlural(arguments[0] as JsonSchema);
if (plural != null)
{
context.Write($"\"{plural}\"");
@@ -40,13 +40,13 @@ namespace KubernetesGenerator
}
}
public string GetPlural(JsonSchema4 definition)
public string GetPlural(JsonSchema definition)
{
var className = classNameHelper.GetClassNameForSchemaDefinition(definition);
return _classNameToPluralMap.GetValueOrDefault(className, null);
}
private Dictionary<string, string> InitClassNameToPluralMap(SwaggerDocument swagger)
private Dictionary<string, string> InitClassNameToPluralMap(OpenApiDocument swagger)
{
var classNameToPluralMap = swagger.Operations
.Where(x => x.Operation.OperationId.StartsWith("list", StringComparison.InvariantCulture))
@@ -54,7 +54,7 @@ namespace KubernetesGenerator
{
PluralName = x.Path.Split("/").Last(),
ClassName = classNameHelper.GetClassNameForSchemaDefinition(x.Operation.Responses["200"]
.ActualResponseSchema),
.ActualResponse.Schema.ActualSchema),
})
.Distinct()
.ToDictionary(x => x.ClassName, x => x.PluralName);

View File

@@ -20,9 +20,9 @@ namespace KubernetesWatchGenerator
{
var outputDirectory = options.OutputPath;
var swaggerCooked = await SwaggerDocument.FromFileAsync(Path.Combine(outputDirectory, "swagger.json"))
var swaggerCooked = await OpenApiDocument.FromFileAsync(Path.Combine(outputDirectory, "swagger.json"))
.ConfigureAwait(false);
var swaggerUnprocessed = await SwaggerDocument
var swaggerUnprocessed = await OpenApiDocument
.FromFileAsync(Path.Combine(outputDirectory, "swagger.json.unprocessed"))
.ConfigureAwait(false);
@@ -45,7 +45,7 @@ namespace KubernetesWatchGenerator
;
builder.RegisterType<PluralHelper>()
.WithParameter(new TypedParameter(typeof(SwaggerDocument), swaggerUnprocessed))
.WithParameter(new TypedParameter(typeof(OpenApiDocument), swaggerUnprocessed))
.AsImplementedInterfaces()
;

View File

@@ -28,9 +28,9 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is SwaggerParameter)
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiParameter)
{
var parameter = arguments[0] as SwaggerParameter;
var parameter = arguments[0] as OpenApiParameter;
if (parameter.Schema?.Reference != null)
{
@@ -47,9 +47,9 @@ namespace KubernetesGenerator
parameter.Format));
}
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonProperty)
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchemaProperty)
{
var property = arguments[0] as JsonProperty;
var property = arguments[0] as JsonSchemaProperty;
context.Write(GetDotNetType(property));
}
else if (arguments != null && arguments.Count > 2 && arguments[0] != null && arguments[1] != null &&
@@ -61,11 +61,11 @@ namespace KubernetesGenerator
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null)
{
context.Write($"ERROR: Expected SwaggerParameter but got {arguments[0].GetType().FullName}");
context.Write($"ERROR: Expected OpenApiParameter but got {arguments[0].GetType().FullName}");
}
else
{
context.Write("ERROR: Expected a SwaggerParameter argument but got none.");
context.Write("ERROR: Expected a OpenApiParameter argument but got none.");
}
}
@@ -147,7 +147,7 @@ namespace KubernetesGenerator
}
}
private string GetDotNetType(JsonSchema4 schema, JsonProperty parent)
private string GetDotNetType(JsonSchema schema, JsonSchemaProperty parent)
{
if (schema != null)
{
@@ -176,11 +176,11 @@ namespace KubernetesGenerator
return GetDotNetType(parent.Type, parent.Name, parent.IsRequired, parent.Format);
}
public string GetDotNetType(JsonProperty p)
public string GetDotNetType(JsonSchemaProperty p)
{
if (p.SchemaReference != null)
if (p.Reference != null)
{
return classNameHelper.GetClassNameForSchemaDefinition(p.SchemaReference);
return classNameHelper.GetClassNameForSchemaDefinition(p.Reference);
}
if (p.IsArray)
@@ -201,7 +201,7 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var operation = arguments?.FirstOrDefault() as SwaggerOperation;
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string style = null;
@@ -214,9 +214,9 @@ namespace KubernetesGenerator
}
}
private string GetReturnType(SwaggerOperation operation, string sytle)
private string GetReturnType(OpenApiOperation operation, string sytle)
{
SwaggerResponse response;
OpenApiResponse response;
if (!operation.Responses.TryGetValue("200", out response))
{
@@ -292,7 +292,7 @@ namespace KubernetesGenerator
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var operation = arguments?.FirstOrDefault() as SwaggerOperation;
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string type = null;
@@ -320,7 +320,7 @@ namespace KubernetesGenerator
public static void IfType(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var property = arguments?.FirstOrDefault() as JsonProperty;
var property = arguments?.FirstOrDefault() as JsonSchemaProperty;
if (property != null)
{
string type = null;

View File

@@ -29,7 +29,7 @@ namespace KubernetesGenerator
public static void IfKindIs(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var parameter = arguments?.FirstOrDefault() as SwaggerParameter;
var parameter = arguments?.FirstOrDefault() as OpenApiParameter;
if (parameter != null)
{
string kind = null;
@@ -38,11 +38,11 @@ namespace KubernetesGenerator
kind = arguments[1] as string;
}
if (kind == "query" && parameter.Kind == SwaggerParameterKind.Query)
if (kind == "query" && parameter.Kind == OpenApiParameterKind.Query)
{
fn(null);
}
else if (kind == "path" && parameter.Kind == SwaggerParameterKind.Path)
else if (kind == "path" && parameter.Kind == OpenApiParameterKind.Path)
{
fn(null);
}

View File

@@ -7,7 +7,7 @@ namespace KubernetesGenerator
{
public class WatchGenerator
{
public void Generate(SwaggerDocument swagger, string outputDirectory)
public void Generate(OpenApiDocument swagger, string outputDirectory)
{
// Generate the Watcher operations
// We skip operations where the name of the class in the C# client could not be determined correctly.