diff --git a/Makefile b/Makefile index 50efe37..23bf763 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ gen-buggy: dotnet run --project OpenApiGen -- Examples/OpenApiGen.config.json Examples/BuggyApi.json generated gen-invest: - dotnet run --project OpenApiGen -- Examples/OpenApiGen.config.json Examples/InvestApi.json generated + dotnet run --project OpenApiGen -- Examples/InvestApi.config.json Examples/InvestApi.json generated gen-art: dotnet run --project OpenApiGen -- Examples/FundApi.config.json Examples/ArtApi.json generated diff --git a/OpenApiGen/Generators/TypeScriptAxiosGenerator.cs b/OpenApiGen/Generators/TypeScriptAxiosGenerator.cs index 730dc40..9305121 100644 --- a/OpenApiGen/Generators/TypeScriptAxiosGenerator.cs +++ b/OpenApiGen/Generators/TypeScriptAxiosGenerator.cs @@ -1,4 +1,3 @@ -using System.Data.Common; using System.Text; using System.Text.RegularExpressions; @@ -175,14 +174,6 @@ private string ParameterPrototype(Parameter param) { return $"{param.Name}{optional}: {tsType}"; } - private static string ParameterQuery(Parameter param, int index) { - if (param.Schema.Default is null) { - return $"${{{param.Name} ? `&{param.Name}=${{encodeURIComponent({param.Name})}}` : ''}}"; - } else { - return $"&{param.Name}=${{encodeURIComponent({param.Name} ?? {param.Schema.Default})}}"; - } - } - private static string ParameterQueryInitializer(Parameter param) { return $"if ({param.Name} !== undefined) __query__.push(`{param.Name}=${{encodeURIComponent({param.Name})}}`);"; } @@ -266,16 +257,23 @@ private string RawGenerateType(int indent, Schema schema, string[] composedRequi sb.Append('}'); return sb.ToString(); } else if (schema is EnumSchema enumSchema) { - return string.Join(" | ", enumSchema.Enum.Select(e => $"\"{e}\"")); + return string.Join(" | ", enumSchema.Enum.Where(e => e is not null).Select(e => $"\"{e}\"")); } else if (schema is PrimitiveSchema primSchema) { - return (primSchema.Type, primSchema.Format) switch { + var types = + (primSchema.Types, primSchema.Type) switch { + (string[], _) => primSchema.Types, + (null, string) => [primSchema.Type], + _ => ["void"] + }; + return string.Join(" | ", types.Select(type => (type, primSchema.Format) switch { ("string", "binary") => "File", ("string", _) => "string", ("integer", _) => "number", ("number", _) => "number", ("boolean", _) => "boolean", + ("null", _) => "null", _ => "void" - }; + })); } else { throw new ApplicationException("Unknown schema type."); } diff --git a/OpenApiGen/OpenApiDef.cs b/OpenApiGen/OpenApiDef.cs index 025fb18..ee27895 100644 --- a/OpenApiGen/OpenApiDef.cs +++ b/OpenApiGen/OpenApiDef.cs @@ -70,12 +70,12 @@ public record Parameter { [JsonConverter(typeof(SchemaConverter))] public abstract record Schema { - public bool? Nullable { get; init; } + public bool? Nullable { get; init; } // deprecated in 3.1 public object? Default { get; init; } } public sealed record EnumSchema : Schema { - public required List Enum { get; init; } + public required List Enum { get; init; } } public sealed record RefSchema : Schema { @@ -105,5 +105,5 @@ public sealed record Discriminator { public sealed record PrimitiveSchema : Schema { public string? Type { get; init; } // "string", "integer", "number", "boolean" public string? Format { get; init; } // "date-time", "uuid", etc. + public string[]? Types { get; init; } // new in 3.1 } -