Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 10 additions & 12 deletions OpenApiGen/Generators/TypeScriptAxiosGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Data.Common;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -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})}}`);";
}
Expand Down Expand Up @@ -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.");
}
Expand Down
6 changes: 3 additions & 3 deletions OpenApiGen/OpenApiDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Enum { get; init; }
public required List<string?> Enum { get; init; }
}

public sealed record RefSchema : Schema {
Expand Down Expand Up @@ -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
}