NSwag
NSwag copied to clipboard
NSwagStudio TypeScript "wrapResponseMethods" is not working
There a TodoController in My Web API.
my nswag
I read your code. https://github.com/RicoSuter/NSwag/blob/85ae862fd6d68173a201a79e0ad06e0be2ec5de1/src/NSwag.CodeGeneration/Models/OperationModelBase.cs
line 272-276
/// <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));
I think "_settings.WrapResponseMethods.Contains(_settings.GenerateControllerName(ControllerName) + "." + ActualOperationName)" should be true.
I expected "SwaggerResponse" is shown, but not.
Maybe you need to use Todo.GetAllAsync, etc.?
Any update on this? I need to access the location header and found the wrap response solution. It works when I wrap all responses but i do not want this since only some POST requests will have the location header.
Sadly it will not generare wrappers if I specify the methods in any of the following notations
-
MyController.GetItem
-
MyController.GetItemAsync
-
<FullNamespace>.MyController.GetItem
-
<FullNamespace>.MyController.GetItemAsync
Just use "classname.action", i think you setup custom class name https://github.com/RicoSuter/NSwag/blob/ec7257bb2b8ea76b95ed31f50b5716c579a5c431/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs#L66
In initial question "TodoService.GetAll","TodoService.GetById" .... Hope it helps
@MathiasReichardt I finally got it working using NSwagStudio 13.16.1.0 by NOT having any quotes (single or double). So in the case above I would type into the box exactly TodoService.GetAll, TodoService.GetById, TodoService.Search, TodoService.Create
.
Noting that in this example the client is using Class Name setting {controller}Service
(my code uses what I think was the default of {controller}Client
. In summary, whatever name is being used for the generated CSharp Client Class should be used.
@RicoSuter after an afternoon of investigation, I was able to confirm that the "wrapResponseMethods" does work; however, the documentation does not adequately describe how to use it. OperationModelBase.WrapResponse will look very different depending on settings in nswagconfig.nswag
https://github.com/RicoSuter/NSwag/blob/92b168a7c10371e0d163c7fbd7900a590aa13c1e/src/NSwag.CodeGeneration/Models/OperationModelBase.cs#L284-L287
In the simplest case, as demonstrated in WrapResponsesTest.cs, "CSharpClientGeneratorSettings.WrapResponseMethods" will NOT work if set to ["ControllerName.MethodName"].
https://github.com/RicoSuter/NSwag/blob/92b168a7c10371e0d163c7fbd7900a590aa13c1e/src/NSwag.CodeGeneration.CSharp.Tests/WrapResponsesTests.cs#L26-L43
This is because ClientGenerateBaseSettings.GenerateControllerName is actually returning {controller}Client
https://github.com/RicoSuter/NSwag/blob/ec7257bb2b8ea76b95ed31f50b5716c579a5c431/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs#L61-L67
So in WrapResponsesTest, we can get the client to generate correctly if instead we set the WrapResponseMethods to ["{controller}Client.MethodName"]
However, in my case, I have set codeGenerators.openApiToCSharpClient.className
in the NSwag config to a custom value, for example "MyProjectClient". In this scenario, ClientGenerateBaseSettings.GenerateControllerName will simply return the ClassName defined in the config settings.
Additionally, the OperationModelBase.ActualOperationName will change depending on the "operationGenerationMode" setting. In my case, I have it set to "SingleClientFromOperationId" to support multiple controllers in the same generated client. This means that the ActualOperationName is NOT the method name, but instead {controller}_MethodName
In summary, the generator works and the setting "wrapResponseMethods" works, but not in the way documented and not in a way that is easy to understand or predict because of all of the possible variations.
Because of my configuration, I have to use ClassName.{controller}_MethodName
to get the desired behavior out of "wrapResponseMethods"
Example of my config
{
"className": "MyProjectClient",
"operationGenerationMode": "SingleClientFromOperationId",
"wrapResponses": true,
"wrapResponseMethods": ["MyProjectClient.Example_SomeMethod"] // {className}.{controller}_{methodName}
}
Edit: I know this issue has "TypeScript" and "NSwagStudio" in the title, and my experience was through using NSwag MSBuild and attempting to generate a C# client, but the behavior outlined above is all in Base Class implementations and will apply to any scenario.