Skip to content

Commit 534a5bf

Browse files
authored
feat: add openapi31 support (#3)
1 parent e86d8c4 commit 534a5bf

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gen-buggy:
1414
dotnet run --project OpenApiGen -- Examples/OpenApiGen.config.json Examples/BuggyApi.json generated
1515

1616
gen-invest:
17-
dotnet run --project OpenApiGen -- Examples/OpenApiGen.config.json Examples/InvestApi.json generated
17+
dotnet run --project OpenApiGen -- Examples/InvestApi.config.json Examples/InvestApi.json generated
1818

1919
gen-art:
2020
dotnet run --project OpenApiGen -- Examples/FundApi.config.json Examples/ArtApi.json generated

OpenApiGen/Generators/TypeScriptAxiosGenerator.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Data.Common;
21
using System.Text;
32
using System.Text.RegularExpressions;
43

@@ -175,14 +174,6 @@ private string ParameterPrototype(Parameter param) {
175174
return $"{param.Name}{optional}: {tsType}";
176175
}
177176

178-
private static string ParameterQuery(Parameter param, int index) {
179-
if (param.Schema.Default is null) {
180-
return $"${{{param.Name} ? `&{param.Name}=${{encodeURIComponent({param.Name})}}` : ''}}";
181-
} else {
182-
return $"&{param.Name}=${{encodeURIComponent({param.Name} ?? {param.Schema.Default})}}";
183-
}
184-
}
185-
186177
private static string ParameterQueryInitializer(Parameter param) {
187178
return $"if ({param.Name} !== undefined) __query__.push(`{param.Name}=${{encodeURIComponent({param.Name})}}`);";
188179
}
@@ -266,16 +257,23 @@ private string RawGenerateType(int indent, Schema schema, string[] composedRequi
266257
sb.Append('}');
267258
return sb.ToString();
268259
} else if (schema is EnumSchema enumSchema) {
269-
return string.Join(" | ", enumSchema.Enum.Select(e => $"\"{e}\""));
260+
return string.Join(" | ", enumSchema.Enum.Where(e => e is not null).Select(e => $"\"{e}\""));
270261
} else if (schema is PrimitiveSchema primSchema) {
271-
return (primSchema.Type, primSchema.Format) switch {
262+
var types =
263+
(primSchema.Types, primSchema.Type) switch {
264+
(string[], _) => primSchema.Types,
265+
(null, string) => [primSchema.Type],
266+
_ => ["void"]
267+
};
268+
return string.Join(" | ", types.Select(type => (type, primSchema.Format) switch {
272269
("string", "binary") => "File",
273270
("string", _) => "string",
274271
("integer", _) => "number",
275272
("number", _) => "number",
276273
("boolean", _) => "boolean",
274+
("null", _) => "null",
277275
_ => "void"
278-
};
276+
}));
279277
} else {
280278
throw new ApplicationException("Unknown schema type.");
281279
}

OpenApiGen/OpenApiDef.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public record Parameter {
7070

7171
[JsonConverter(typeof(SchemaConverter))]
7272
public abstract record Schema {
73-
public bool? Nullable { get; init; }
73+
public bool? Nullable { get; init; } // deprecated in 3.1
7474
public object? Default { get; init; }
7575
}
7676

7777
public sealed record EnumSchema : Schema {
78-
public required List<string> Enum { get; init; }
78+
public required List<string?> Enum { get; init; }
7979
}
8080

8181
public sealed record RefSchema : Schema {
@@ -105,5 +105,5 @@ public sealed record Discriminator {
105105
public sealed record PrimitiveSchema : Schema {
106106
public string? Type { get; init; } // "string", "integer", "number", "boolean"
107107
public string? Format { get; init; } // "date-time", "uuid", etc.
108+
public string[]? Types { get; init; } // new in 3.1
108109
}
109-

0 commit comments

Comments
 (0)