forked from RicoSuter/NSwag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeScriptOperationModel.cs
223 lines (187 loc) · 10.1 KB
/
TypeScriptOperationModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//-----------------------------------------------------------------------
// <copyright file="TypeScriptOperationModel.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------
using NJsonSchema;
using NJsonSchema.CodeGeneration;
using NSwag.CodeGeneration.Models;
namespace NSwag.CodeGeneration.TypeScript.Models
{
/// <summary>The TypeScript operation model.</summary>
public class TypeScriptOperationModel : OperationModelBase<TypeScriptParameterModel, TypeScriptResponseModel>
{
private readonly TypeScriptClientGeneratorSettings _settings;
private readonly TypeScriptClientGenerator _generator;
private readonly OpenApiOperation _operation;
/// <summary>Initializes a new instance of the <see cref="TypeScriptOperationModel" /> class.</summary>
/// <param name="operation">The operation.</param>
/// <param name="settings">The settings.</param>
/// <param name="generator">The generator.</param>
/// <param name="resolver">The resolver.</param>
public TypeScriptOperationModel(
OpenApiOperation operation,
TypeScriptClientGeneratorSettings settings,
TypeScriptClientGenerator generator,
TypeResolverBase resolver)
: base(null, operation, resolver, generator, settings)
{
_operation = operation;
_settings = settings;
_generator = generator;
var parameters = GetActualParameters();
if (settings.GenerateOptionalParameters)
{
parameters =
[
.. parameters
.OrderBy(p => !p.IsRequired)
.ThenBy(p => p.Position ?? 0)
];
}
Parameters = parameters
.Select(parameter =>
new TypeScriptParameterModel(parameter.Name,
GetParameterVariableName(parameter, _operation.Parameters), ResolveParameterType(parameter),
parameter, parameters, _settings, _generator, resolver))
.ToList();
}
/// <summary>Gets the actual name of the operation (language specific).</summary>
public override string ActualOperationName => ConversionUtilities.ConvertToLowerCamelCase(OperationName, false)
+ (MethodAccessModifier == "protected " ? "Core" : string.Empty);
/// <summary>Gets the actual name of the operation (language specific).</summary>
public string ActualOperationNameUpper => ConversionUtilities.ConvertToUpperCamelCase(OperationName, false);
/// <summary>Gets or sets the type of the result.</summary>
public override string ResultType
{
get
{
var response = GetSuccessResponse();
var isNullable = response.Value?.IsNullable(_settings.CodeGeneratorSettings.SchemaType) == true;
var resultType = isNullable && SupportsStrictNullChecks && UnwrappedResultType != "void" && UnwrappedResultType != "null" ?
UnwrappedResultType + " | null" :
UnwrappedResultType;
if (WrapResponse)
{
return _settings.ResponseClass.Replace("{controller}", ControllerName) + "<" + resultType + ">";
}
else
{
return resultType;
}
}
}
/// <summary>Gets a value indicating whether the operation requires mappings for DTO generation.</summary>
public bool RequiresMappings => Responses.Any(r => r.HasType && r.ActualResponseSchema.UsesComplexObjectSchema());
/// <summary>Gets a value indicating whether the target TypeScript version supports strict null checks.</summary>
public bool SupportsStrictNullChecks => _settings.TypeScriptGeneratorSettings.TypeScriptVersion >= 2.0m;
/// <summary>Gets a value indicating whether to handle references.</summary>
public bool HandleReferences => _settings.TypeScriptGeneratorSettings.HandleReferences;
/// <summary>Gets a value indicating whether the template can request blobs.</summary>
public bool CanRequestBlobs => !IsJQuery;
/// <summary>Gets a value indicating whether to use blobs with Angular.</summary>
public bool RequestAngularBlobs => IsAngular && IsFile;
/// <summary>Gets a value indicating whether to use blobs with AngularJS.</summary>
public bool RequestAngularJSBlobs => IsAngularJS && IsFile;
/// <summary>Gets a value indicating whether to render for AngularJS.</summary>
public bool IsAngularJS => _settings.Template == TypeScriptTemplate.AngularJS;
/// <summary>Gets a value indicating whether to render for Angular2.</summary>
public bool IsAngular => _settings.Template == TypeScriptTemplate.Angular;
/// <summary>Gets a value indicating whether to render for JQuery.</summary>
public bool IsJQuery => _settings.Template is TypeScriptTemplate.JQueryCallbacks or TypeScriptTemplate.JQueryPromises;
/// <summary>Gets a value indicating whether to render for Fetch or Aurelia</summary>
public bool IsFetchOrAurelia => _settings.Template is TypeScriptTemplate.Fetch or TypeScriptTemplate.Aurelia;
/// <summary>Gets a value indicating whether to use HttpClient with the Angular template.</summary>
public bool UseAngularHttpClient => IsAngular && _settings.HttpClass == HttpClass.HttpClient;
/// <summary>Gets or sets the type of the exception.</summary>
public override string ExceptionType
{
get
{
if (_operation.ActualResponses.All(r => HttpUtilities.IsSuccessStatusCode(r.Key)))
{
return "string";
}
return string.Join(" | ", _operation.ActualResponses
.Where(r => !HttpUtilities.IsSuccessStatusCode(r.Key) && r.Value.Schema != null)
.Select(r => _generator.GetTypeName(r.Value.Schema, r.Value.IsNullable(_settings.CodeGeneratorSettings.SchemaType), "Exception"))
.Concat(["string"]));
}
}
/// <summary>Gets the method's access modifier.</summary>
public string MethodAccessModifier
{
get
{
var controllerName = _settings.GenerateControllerName(ControllerName);
if (_settings.ProtectedMethods?.Contains(controllerName + "." + ConversionUtilities.ConvertToLowerCamelCase(OperationName, false)) == true)
{
return "protected ";
}
return "";
}
}
/// <summary>Gets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses => _settings.WrapResponses;
/// <summary>Gets the response class name.</summary>
public string ResponseClass => _settings.ResponseClass.Replace("{controller}", ControllerName);
/// <summary>Resolves the type of the parameter.</summary>
/// <param name="parameter">The parameter.</param>
/// <returns>The parameter type name.</returns>
protected override string ResolveParameterType(OpenApiParameter parameter)
{
if (parameter.IsBinaryBodyParameter)
{
return "Blob";
// TODO: Use HasBinaryBodyWithMultipleMimeTypes => how to provide content type in TS?
//return parameter.HasBinaryBodyWithMultipleMimeTypes ? "FileParameter" : "Blob";
}
var schema = parameter.ActualSchema;
if (schema.Type == JsonObjectType.Array && schema.Item.IsBinary)
{
return "FileParameter[]";
}
if (schema.IsBinary)
{
if (parameter.CollectionFormat == OpenApiParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array))
{
return "FileParameter[]";
}
return "FileParameter";
}
if (_settings.TypeScriptGeneratorSettings.UseLeafType && schema.ActualDiscriminatorObject != null)
{
var types = schema.ActualDiscriminatorObject.Mapping
.Select(x => new OpenApiParameter
{
Name = parameter.Name,
Kind = parameter.Kind,
IsRequired = parameter.IsRequired,
IsNullableRaw = parameter.IsNullableRaw,
Description = parameter.Description,
Schema = x.Value
})
.Select(ResolveParameterType);
return string.Join(" | ", types);
}
return base.ResolveParameterType(parameter);
}
/// <summary>Creates the response model.</summary>
/// <param name="operation">The operation.</param>
/// <param name="statusCode">The status code.</param>
/// <param name="response">The response.</param>
/// <param name="exceptionSchema">The exception schema.</param>
/// <param name="generator">The generator.</param>
/// <param name="resolver">The resolver.</param>
/// <param name="settings">The settings.</param>
/// <returns></returns>
protected override TypeScriptResponseModel CreateResponseModel(OpenApiOperation operation, string statusCode, OpenApiResponse response,
JsonSchema exceptionSchema, IClientGenerator generator, TypeResolverBase resolver, ClientGeneratorBaseSettings settings)
{
return new TypeScriptResponseModel(this, operation, statusCode, response, response == GetSuccessResponse().Value,
exceptionSchema, generator, resolver, (TypeScriptClientGeneratorSettings)settings);
}
}
}