@team-supercharge/oasg 18.0.2 → 18.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -33
- package/bin/oasg +2 -0
- package/config.schema.yml +37 -0
- package/package.json +1 -1
- package/targets/dotnet/generate.sh +2 -11
- package/targets/dotnet/publish.sh +2 -17
- package/targets/dotnet/templates/Project.csproj.mustache +39 -0
- package/targets/dotnet-common.sh +48 -0
- package/targets/dotnet-system-text-json/generate.sh +6 -0
- package/targets/dotnet-system-text-json/generator-config.json +15 -0
- package/targets/dotnet-system-text-json/publish.sh +6 -0
- package/targets/dotnet-system-text-json/templates/netcore_project.mustache +85 -0
- package/targets/dotnet-webapi/generate.sh +2 -11
- package/targets/dotnet-webapi/publish.sh +2 -17
- package/targets/dotnet-webapi/templates/Project.csproj.mustache +61 -0
- package/targets/dotnet-webapi-system-text-json/README.md +234 -0
- package/targets/dotnet-webapi-system-text-json/generate.sh +33 -0
- package/targets/dotnet-webapi-system-text-json/generator-config.json +29 -0
- package/targets/dotnet-webapi-system-text-json/publish.sh +6 -0
- package/targets/dotnet-webapi-system-text-json/templates/Project.csproj.mustache +65 -0
- package/targets/dotnet-webapi-system-text-json/templates/controller.mustache +7 -0
- package/targets/dotnet-webapi-system-text-json/templates/endpointParam.mustache +1 -0
- package/targets/dotnet-webapi-system-text-json/templates/minimalEndpoints.mustache +118 -0
- package/targets/dotnet-webapi-system-text-json/templates/model.mustache +206 -0
- package/targets/dotnet-webapi-system-text-json/templates/paramType.mustache +1 -0
- package/targets/dotnet-webapi-system-text-json/templates/serviceCollectionExtensions.mustache +66 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
{{>partial_header}}
|
|
2
|
+
using System;
|
|
3
|
+
using System.Linq;
|
|
4
|
+
using System.Text;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.ComponentModel;
|
|
7
|
+
using System.ComponentModel.DataAnnotations;
|
|
8
|
+
using System.Runtime.Serialization;
|
|
9
|
+
{{#useNewtonsoft}}
|
|
10
|
+
using Newtonsoft.Json;
|
|
11
|
+
{{/useNewtonsoft}}
|
|
12
|
+
{{^useNewtonsoft}}
|
|
13
|
+
using System.Text.Json;
|
|
14
|
+
using System.Text.Json.Serialization;
|
|
15
|
+
{{/useNewtonsoft}}
|
|
16
|
+
{{#models}}
|
|
17
|
+
{{#model}}
|
|
18
|
+
{{#discriminator}}
|
|
19
|
+
using JsonSubTypes;
|
|
20
|
+
{{#useSwashbuckle}}
|
|
21
|
+
using Swashbuckle.AspNetCore.Annotations;
|
|
22
|
+
{{/useSwashbuckle}}
|
|
23
|
+
{{/discriminator}}
|
|
24
|
+
{{/model}}
|
|
25
|
+
{{/models}}
|
|
26
|
+
using {{packageName}}.Converters;
|
|
27
|
+
|
|
28
|
+
{{#models}}
|
|
29
|
+
{{#model}}
|
|
30
|
+
namespace {{modelPackage}}
|
|
31
|
+
{ {{#isEnum}}{{>enumClass}}{{/isEnum}}{{^isEnum}}
|
|
32
|
+
/// <summary>
|
|
33
|
+
/// {{description}}
|
|
34
|
+
/// </summary>
|
|
35
|
+
[DataContract]
|
|
36
|
+
{{#discriminator}}
|
|
37
|
+
{{#useNewtonsoft}}
|
|
38
|
+
{{#mappedModels.size}}
|
|
39
|
+
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]
|
|
40
|
+
{{/mappedModels.size}}
|
|
41
|
+
{{/useNewtonsoft}}
|
|
42
|
+
{{#useSwashbuckle}}
|
|
43
|
+
[SwaggerDiscriminator("{{{discriminatorName}}}")]
|
|
44
|
+
{{/useSwashbuckle}}
|
|
45
|
+
{{#mappedModels}}
|
|
46
|
+
[JsonSubtypes.KnownSubType(typeof({{{modelName}}}), "{{^vendorExtensions.x-discriminator-value}}{{{mappingName}}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{.}}}{{/vendorExtensions.x-discriminator-value}}")]
|
|
47
|
+
{{#useSwashbuckle}}
|
|
48
|
+
[SwaggerSubType(typeof({{{modelName}}}), DiscriminatorValue = "{{^vendorExtensions.x-discriminator-value}}{{{mappingName}}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{.}}}{{/vendorExtensions.x-discriminator-value}}")]
|
|
49
|
+
{{/useSwashbuckle}}
|
|
50
|
+
{{/mappedModels}}
|
|
51
|
+
{{/discriminator}}
|
|
52
|
+
public {{#modelClassModifier}}{{.}} {{/modelClassModifier}}class {{classname}} {{#parent}}: {{{.}}}{{^pocoModels}}, {{/pocoModels}}{{/parent}}{{^pocoModels}}{{^parent}}: {{/parent}}IEquatable<{{classname}}>{{/pocoModels}}
|
|
53
|
+
{
|
|
54
|
+
{{#vars}}
|
|
55
|
+
{{#items.isEnum}}
|
|
56
|
+
{{#items}}
|
|
57
|
+
{{^complexType}}
|
|
58
|
+
{{>enumClass}}
|
|
59
|
+
{{/complexType}}
|
|
60
|
+
{{/items}}
|
|
61
|
+
{{/items.isEnum}}
|
|
62
|
+
{{#isEnum}}
|
|
63
|
+
{{^complexType}}
|
|
64
|
+
{{>enumClass}}
|
|
65
|
+
{{/complexType}}
|
|
66
|
+
{{/isEnum}}
|
|
67
|
+
/// <summary>
|
|
68
|
+
/// {{description}}{{^description}}Gets or Sets {{{name}}}{{/description}}
|
|
69
|
+
/// </summary>{{#description}}
|
|
70
|
+
/// <value>{{.}}</value>{{/description}}{{#example}}
|
|
71
|
+
/* <example>{{.}}</example> */{{/example}}{{#required}}
|
|
72
|
+
[Required]{{/required}}{{#pattern}}
|
|
73
|
+
[RegularExpression("{{{.}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}
|
|
74
|
+
[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}}
|
|
75
|
+
[MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}}
|
|
76
|
+
[MaxLength({{.}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}
|
|
77
|
+
[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}
|
|
78
|
+
[DataMember(Name="{{baseName}}", EmitDefaultValue={{#isNullable}}true{{/isNullable}}{{^isNullable}}{{#vendorExtensions.x-is-value-type}}true{{/vendorExtensions.x-is-value-type}}{{^vendorExtensions.x-is-value-type}}false{{/vendorExtensions.x-is-value-type}}{{/isNullable}})]
|
|
79
|
+
{{#isEnum}}
|
|
80
|
+
public {{{datatypeWithEnum}}}{{#isNullable}}{{^required}}?{{/required}}{{/isNullable}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
|
|
81
|
+
{{/isEnum}}
|
|
82
|
+
{{^isEnum}}
|
|
83
|
+
public {{{dataType}}}{{#nullableReferenceTypes}}{{^isContainer}}{{^required}}{{^isNullable}}?{{/isNullable}}{{/required}}{{/isContainer}}{{/nullableReferenceTypes}} {{name}} { get; set; }{{#defaultValue}} = {{{.}}};{{/defaultValue}}
|
|
84
|
+
{{/isEnum}}
|
|
85
|
+
{{^-last}}
|
|
86
|
+
|
|
87
|
+
{{/-last}}
|
|
88
|
+
{{/vars}}
|
|
89
|
+
|
|
90
|
+
{{^pocoModels}}
|
|
91
|
+
/// <summary>
|
|
92
|
+
/// Returns the string presentation of the object
|
|
93
|
+
/// </summary>
|
|
94
|
+
/// <returns>String presentation of the object</returns>
|
|
95
|
+
public override string ToString()
|
|
96
|
+
{
|
|
97
|
+
var sb = new StringBuilder();
|
|
98
|
+
sb.Append("class {{classname}} {\n");
|
|
99
|
+
{{#vars}}
|
|
100
|
+
sb.Append(" {{name}}: ").Append({{name}}).Append("\n");
|
|
101
|
+
{{/vars}}
|
|
102
|
+
sb.Append("}\n");
|
|
103
|
+
return sb.ToString();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// <summary>
|
|
107
|
+
/// Returns the JSON string presentation of the object
|
|
108
|
+
/// </summary>
|
|
109
|
+
/// <returns>JSON string presentation of the object</returns>
|
|
110
|
+
public {{#parent}}{{^isMap}}{{^isArray}}new {{/isArray}}{{/isMap}}{{/parent}}string ToJson()
|
|
111
|
+
{
|
|
112
|
+
{{#useNewtonsoft}}
|
|
113
|
+
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
|
114
|
+
{{/useNewtonsoft}}
|
|
115
|
+
{{^useNewtonsoft}}
|
|
116
|
+
return JsonSerializer.Serialize(this, {{classname}}SerializationContext.Default.{{classname}});
|
|
117
|
+
{{/useNewtonsoft}}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// <summary>
|
|
121
|
+
/// Returns true if objects are equal
|
|
122
|
+
/// </summary>
|
|
123
|
+
/// <param name="obj">Object to be compared</param>
|
|
124
|
+
/// <returns>Boolean</returns>
|
|
125
|
+
public override bool Equals(object obj)
|
|
126
|
+
{
|
|
127
|
+
if (obj is null) return false;
|
|
128
|
+
if (ReferenceEquals(this, obj)) return true;
|
|
129
|
+
return obj.GetType() == GetType() && Equals(({{classname}})obj);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// <summary>
|
|
133
|
+
/// Returns true if {{classname}} instances are equal
|
|
134
|
+
/// </summary>
|
|
135
|
+
/// <param name="other">Instance of {{classname}} to be compared</param>
|
|
136
|
+
/// <returns>Boolean</returns>
|
|
137
|
+
public bool Equals({{classname}} other)
|
|
138
|
+
{
|
|
139
|
+
if (other is null) return false;
|
|
140
|
+
if (ReferenceEquals(this, other)) return true;
|
|
141
|
+
|
|
142
|
+
return {{#vars}}{{^isContainer}}
|
|
143
|
+
(
|
|
144
|
+
{{name}} == other.{{name}} ||
|
|
145
|
+
{{^vendorExtensions.x-is-value-type}}{{name}} != null &&{{/vendorExtensions.x-is-value-type}}
|
|
146
|
+
{{name}}.Equals(other.{{name}})
|
|
147
|
+
){{^-last}} && {{/-last}}{{/isContainer}}{{#isContainer}}
|
|
148
|
+
(
|
|
149
|
+
{{name}} == other.{{name}} ||
|
|
150
|
+
{{^vendorExtensions.x-is-value-type}}{{name}} != null &&
|
|
151
|
+
other.{{name}} != null &&
|
|
152
|
+
{{/vendorExtensions.x-is-value-type}}{{name}}.SequenceEqual(other.{{name}})
|
|
153
|
+
){{^-last}} && {{/-last}}{{/isContainer}}{{/vars}}{{^vars}}false{{/vars}};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// <summary>
|
|
157
|
+
/// Gets the hash code
|
|
158
|
+
/// </summary>
|
|
159
|
+
/// <returns>Hash code</returns>
|
|
160
|
+
public override int GetHashCode()
|
|
161
|
+
{
|
|
162
|
+
unchecked // Overflow is fine, just wrap
|
|
163
|
+
{
|
|
164
|
+
var hashCode = 41;
|
|
165
|
+
// Suitable nullity checks etc, of course :)
|
|
166
|
+
{{#vars}}
|
|
167
|
+
{{^vendorExtensions.x-is-value-type}}if ({{name}} != null){{/vendorExtensions.x-is-value-type}}
|
|
168
|
+
hashCode = hashCode * 59 + {{name}}.GetHashCode();
|
|
169
|
+
{{/vars}}
|
|
170
|
+
return hashCode;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#region Operators
|
|
175
|
+
#pragma warning disable 1591
|
|
176
|
+
|
|
177
|
+
public static bool operator ==({{classname}} left, {{classname}} right)
|
|
178
|
+
{
|
|
179
|
+
return Equals(left, right);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public static bool operator !=({{classname}} left, {{classname}} right)
|
|
183
|
+
{
|
|
184
|
+
return !Equals(left, right);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#pragma warning restore 1591
|
|
188
|
+
#endregion Operators
|
|
189
|
+
{{/pocoModels}}
|
|
190
|
+
}
|
|
191
|
+
{{^useNewtonsoft}}
|
|
192
|
+
|
|
193
|
+
/// <summary>
|
|
194
|
+
/// System.Text.Json source-generation context for <see cref="{{classname}}" />.
|
|
195
|
+
/// Used by ToJson() so (de)serialization is trim- and AOT-safe.
|
|
196
|
+
/// </summary>
|
|
197
|
+
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
198
|
+
[JsonSerializable(typeof({{classname}}))]
|
|
199
|
+
internal partial class {{classname}}SerializationContext : JsonSerializerContext
|
|
200
|
+
{
|
|
201
|
+
}
|
|
202
|
+
{{/useNewtonsoft}}
|
|
203
|
+
{{/isEnum}}
|
|
204
|
+
{{/model}}
|
|
205
|
+
{{/models}}
|
|
206
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{{#isQueryParam}}{{#isArray}}string[]{{/isArray}}{{^isArray}}{{{dataType}}}{{/isArray}}{{/isQueryParam}}{{^isQueryParam}}{{{dataType}}}{{/isQueryParam}}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// <auto-generated/>
|
|
2
|
+
using System;
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Reflection;
|
|
5
|
+
using System.Runtime.Serialization;
|
|
6
|
+
using System.Text.Json;
|
|
7
|
+
using System.Text.Json.Serialization;
|
|
8
|
+
using Microsoft.AspNetCore.Http.Json;
|
|
9
|
+
using Microsoft.Extensions.DependencyInjection;
|
|
10
|
+
|
|
11
|
+
namespace {{packageName}}
|
|
12
|
+
{
|
|
13
|
+
/// <summary>
|
|
14
|
+
/// Registers the System.Text.Json options required by this generated package.
|
|
15
|
+
/// Call <c>builder.Services.Add{{packageName}}JsonOptions()</c> in Program.cs.
|
|
16
|
+
/// </summary>
|
|
17
|
+
public static class {{packageName}}ServiceCollectionExtensions
|
|
18
|
+
{
|
|
19
|
+
public static IServiceCollection Add{{packageName}}JsonOptions(this IServiceCollection services)
|
|
20
|
+
{
|
|
21
|
+
services.Configure<JsonOptions>(opts =>
|
|
22
|
+
opts.SerializerOptions.Converters.Add(new EnumMemberJsonConverterFactory()));
|
|
23
|
+
return services;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// <summary>
|
|
28
|
+
/// Converts enums using their <see cref="EnumMemberAttribute" /> wire values (falling back to
|
|
29
|
+
/// the member name), so JSON like <c>"available"</c> round-trips to <c>StatusEnum.Available</c>.
|
|
30
|
+
/// </summary>
|
|
31
|
+
internal sealed class EnumMemberJsonConverterFactory : JsonConverterFactory
|
|
32
|
+
{
|
|
33
|
+
public override bool CanConvert(Type typeToConvert) => typeToConvert.IsEnum;
|
|
34
|
+
|
|
35
|
+
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
|
36
|
+
(JsonConverter?)Activator.CreateInstance(
|
|
37
|
+
typeof(EnumMemberJsonConverter<>).MakeGenericType(typeToConvert));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
internal sealed class EnumMemberJsonConverter<T> : JsonConverter<T> where T : struct, Enum
|
|
41
|
+
{
|
|
42
|
+
private static readonly Dictionary<string, T> _read;
|
|
43
|
+
private static readonly Dictionary<T, string> _write;
|
|
44
|
+
|
|
45
|
+
static EnumMemberJsonConverter()
|
|
46
|
+
{
|
|
47
|
+
_read = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
|
|
48
|
+
_write = new Dictionary<T, string>();
|
|
49
|
+
foreach (var field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
|
|
50
|
+
{
|
|
51
|
+
var wireValue = field.GetCustomAttribute<EnumMemberAttribute>()?.Value ?? field.Name;
|
|
52
|
+
var enumValue = (T)field.GetValue(null)!;
|
|
53
|
+
_read[wireValue] = enumValue;
|
|
54
|
+
_write[enumValue] = wireValue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
|
|
59
|
+
_read.TryGetValue(reader.GetString() ?? string.Empty, out var value)
|
|
60
|
+
? value
|
|
61
|
+
: throw new JsonException($"Unknown value '{reader.GetString()}' for {typeof(T).Name}");
|
|
62
|
+
|
|
63
|
+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
|
|
64
|
+
writer.WriteStringValue(_write.TryGetValue(value, out var s) ? s : value.ToString());
|
|
65
|
+
}
|
|
66
|
+
}
|