Permalink
Browse files

WrapResponses for TypeScript, #894

  • Loading branch information...
RSuter committed Aug 25, 2017
1 parent 6f5968b commit 523028bd56a7d5ab76b445869f6d27e68abd8ea0
View
@@ -1,20 +1,20 @@
The MIT License (MIT)
Copyright (c) 2017 Rico Suter
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The MIT License (MIT)
Copyright (c) 2017 Rico Suter
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -103,12 +103,6 @@ public override string ResultType
}
}
/// <summary>Gets a value indicating whether to wrap the response of this operation.</summary>
public bool WrapResponse => _settings.WrapResponses && (
_settings.WrapResponseMethods == null ||
_settings.WrapResponseMethods.Length == 0 ||
_settings.WrapResponseMethods.Contains(_settings.GenerateControllerName(ControllerName) + "." + ActualOperationName));
/// <summary>Gets or sets the type of the exception.</summary>
public override string ExceptionType
{
@@ -11,19 +11,22 @@ namespace NSwag.CodeGeneration.CSharp.Models
/// <summary>Base class for the CSharp models</summary>
public abstract class CSharpTemplateModelBase
{
private readonly string _controllerName;
private readonly SwaggerToCSharpGeneratorSettings _settings;
/// <summary>Initializes a new instance of the <see cref="CSharpTemplateModelBase"/> class.</summary>
/// <param name="controllerName">Name of the controller.</param>
/// <param name="settings">The settings.</param>
protected CSharpTemplateModelBase(string controllerName, SwaggerToCSharpGeneratorSettings settings)
{
ResponseClass = settings.ResponseClass.Replace("{controller}", controllerName);
WrapResponses = settings.WrapResponses;
_controllerName = controllerName;
_settings = settings;
}
/// <summary>Gets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses { get; set; }
public bool WrapResponses => _settings.WrapResponses;
/// <summary>Gets the response class name.</summary>
public string ResponseClass { get; protected set; }
public string ResponseClass => _settings.ResponseClass.Replace("{controller}", _controllerName);
}
}
@@ -25,9 +25,6 @@ protected SwaggerToCSharpGeneratorSettings()
NullHandling = NullHandling.Swagger,
TemplateFactory = new DefaultTemplateFactory()
};
GenerateResponseClasses = true;
ResponseClass = "SwaggerResponse";
}
/// <summary>Gets or sets the CSharp generator settings.</summary>
@@ -41,17 +38,5 @@ protected SwaggerToCSharpGeneratorSettings()
/// <summary>Gets the code generator settings.</summary>
public override CodeGeneratorSettingsBase CodeGeneratorSettings => CSharpGeneratorSettings;
/// <summary>Gets or sets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses { get; set; }
/// <summary>Gets or sets the list of methods where responses are wrapped ("ControllerName.MethodName", WrapResponses must be true).</summary>
public string[] WrapResponseMethods { get; set; }
/// <summary>Gets or sets a value indicating whether to generate the response classes (only needed when WrapResponses == true, default: true).</summary>
public bool GenerateResponseClasses { get; set; }
/// <summary>Gets or sets the name of the response class (supports the '{controller}' placeholder).</summary>
public string ResponseClass { get; set; }
}
}
@@ -42,10 +42,6 @@ public class TypeScriptClientTemplateModel
Operations = operations;
BaseClass = _settings.ClientBaseClass?.Replace("{controller}", controllerName);
ResponseClass = settings.ResponseClass;
//ResponseClass = settings.ResponseClass.Replace("{controller}", controllerName);
WrapResponses = settings.WrapResponses;
}
/// <summary>Gets the class name.</summary>
@@ -116,11 +112,5 @@ public class TypeScriptClientTemplateModel
/// <summary>Gets or sets a value indicating whether DTO exceptions are wrapped in a SwaggerException instance.</summary>
public bool WrapDtoExceptions => _settings.WrapDtoExceptions;
/// <summary>Gets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses { get; set; }
/// <summary>Gets the response class name.</summary>
public string ResponseClass { get; protected set; }
}
}
@@ -72,16 +72,17 @@ public class TypeScriptFileTemplateModel
/// <summary>Gets or sets 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 => this._settings.ResponseClass;
/// <summary>Gets or sets a value indicating whether to generate the response class (only applied when WrapResponses == true, default: true).</summary>
public bool GenerateResponseClasses => _settings.GenerateResponseClasses;
/// <summary>Gets the response class names.</summary>
public IEnumerable<string> ResponseClassNames {
get {
if (_settings.OperationNameGenerator.SupportsMultipleClients) {
public IEnumerable<string> ResponseClassNames
{
get
{
// TODO: Merge with ResponseClassNames of C#
if (_settings.OperationNameGenerator.SupportsMultipleClients)
{
return _document.Operations
.GroupBy(o => _settings.OperationNameGenerator.GetClientName(_document, o.Path, o.Method, o.Operation))
.Select(g => _settings.ResponseClass.Replace("{controller}", g.Key))
@@ -15,7 +15,7 @@ namespace NSwag.CodeGeneration.TypeScript.Models
{
/// <summary>The TypeScript operation model.</summary>
public class TypeScriptOperationModel : OperationModelBase<TypeScriptParameterModel, TypeScriptResponseModel>
{
{
private readonly SwaggerToTypeScriptClientGeneratorSettings _settings;
private readonly SwaggerToTypeScriptClientGenerator _generator;
private readonly SwaggerOperation _operation;
@@ -31,7 +31,7 @@ public class TypeScriptOperationModel : OperationModelBase<TypeScriptParameterMo
SwaggerToTypeScriptClientGenerator generator,
ITypeResolver resolver)
: base(null, operation, resolver, generator, settings)
{
{
_operation = operation;
_settings = settings;
_generator = generator;
@@ -55,23 +55,25 @@ public class TypeScriptOperationModel : OperationModelBase<TypeScriptParameterMo
public string ActualOperationNameUpper => ConversionUtilities.ConvertToUpperCamelCase(OperationName, false);
/// <summary>Gets or sets the type of the result.</summary>
public override string ResultType {
get {
var resultType = SupportsStrictNullChecks && UnwrappedResultType != "void" && UnwrappedResultType != "null"
? UnwrappedResultType + " | null"
: UnwrappedResultType;
if (_settings != null && _settings.WrapResponses && _settings.TemplateSupportsWrapResponses) {
resultType = resultType == "void"
? _settings.ResponseClass + "Base"
: _settings.ResponseClass + "<" + resultType.Replace("{controller}", ControllerName) + ">";
public override string ResultType
{
get
{
var resultType = SupportsStrictNullChecks && UnwrappedResultType != "void" && UnwrappedResultType != "null" ?
UnwrappedResultType + " | null" :
UnwrappedResultType;
if (WrapResponse)
{
return _settings.ResponseClass.Replace("{controller}", ControllerName) + "<" + resultType + ">";
}
else
{
return resultType;
}
return resultType;
}
}
public bool WrapResponses => this._settings.WrapResponses;
public string ResponseClass => this._settings.ResponseClass;
/// <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());
@@ -132,6 +134,13 @@ public string MethodAccessModifier
public bool IsFetchOrAurelia => _settings.Template == TypeScriptTemplate.Fetch ||
_settings.Template == TypeScriptTemplate.Aurelia;
/// <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>
@@ -30,8 +30,6 @@ public SwaggerToTypeScriptClientGeneratorSettings()
};
BaseUrlTokenName = "API_BASE_URL";
ImportRequiredTypes = true;
ResponseClass = "SwaggerResponse";
this.GenerateResponseClasses = true;
}
/// <summary>Gets or sets the TypeScript generator settings.</summary>
@@ -43,8 +41,6 @@ public SwaggerToTypeScriptClientGeneratorSettings()
/// <summary>Gets or sets the output template.</summary>
public TypeScriptTemplate Template { get; set; }
public bool TemplateSupportsWrapResponses => Template == TypeScriptTemplate.Angular;
/// <summary>Gets or sets the promise type.</summary>
public PromiseType PromiseType { get; set; }
@@ -75,15 +71,6 @@ public SwaggerToTypeScriptClientGeneratorSettings()
/// <summary>Gets or sets a value indicating whether to use the 'getBaseUrl(defaultUrl: string)' from the base class (default: false).</summary>
public bool UseGetBaseUrlMethod { get; set; }
/// <summary>Gets or sets a value indicating whether to wrap success responses to allow full response access (experimental).</summary>
public bool WrapResponses { get; set; }
/// <summary>Gets or sets a value indicating whether to generate the response classes (only needed when WrapResponses == true, default: true).</summary>
public bool GenerateResponseClasses { get; set; }
/// <summary>Gets or sets the name of the response class (supports the '{controller}' placeholder).</summary>
public string ResponseClass { get; set; }
internal ITemplate CreateTemplate(object model)
{
if (Template == TypeScriptTemplate.Aurelia)
Oops, something went wrong.

0 comments on commit 523028b

Please sign in to comment.