question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NSwagStudio TypeScript "wrapResponseMethods" is not working

See original GitHub issue

There a TodoController in My Web API. image

my nswag image

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
definedriskcommented, Aug 19, 2022

@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.

0reactions
bit-nibble-bytecommented, Aug 9, 2023

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NSwagStudio TypeScript "wrapResponseMethods" is not ...
NSwagStudio TypeScript "wrapResponseMethods" is not working. NSwag. 20 February 2020 Posted by p770820. There a TodoController in My Web API.
Read more >
Nswag Typescript API client InjectionToken is not being ...
The "baseUrl" parameter is always null like if the injection is not working, the provider configuration seems to be missing something.
Read more >
Using OpenApiReference To Generate Open API Client ...
WrapResponseMethods, string[], Empty Array, List of methods where responses are wrapped ('ControllerName.MethodName', WrapResponses must be ...
Read more >
Generate TypeScript and C# clients with NSwag based on ...
Learn how to generate TypeScript and C# API clients with NSwag to reduce your workload when building a project.
Read more >
Untitled
reddit https://github.com/RicoSuter/NSwag/issues/2384 Nswag specify ... 2020 · NSwagStudio TypeScript "wrapResponseMethods" is not working #2699 Open ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found