Nswag: How to avoid adding authorization header to the generated c# code

Created on 23 Jun 2017  路  10Comments  路  Source: RicoSuter/NSwag

Hi guys,
Is it possible to avoid adding the authorization header to every method of the client?
I'm using JWT and I want to add the headers myself only once on a base class.

Also, how can I set the name of the generated class?

Tks!

question

Most helpful comment

No, there is no such functionality at the moment. ATM, you need to preprocess the swagger spec before feeding it into the code generator

Thanks, that was easy enough :)

        private static string GenerateApiClientCode(string swaggerJsonContent)
        {
            SwaggerDocument swaggerDoc = SwaggerDocument.FromJsonAsync(swaggerJsonContent).ResultEx();

            //Remove auth header in operations since we supply it in API client base class
            swaggerDoc.Operations.ForEach(operation =>
            {
                SwaggerParameter headerParam = operation.Operation.Parameters.FirstOrDefault((param) =>
                    param.Kind == SwaggerParameterKind.Header &&
                    param.Name == "auth-token");
                if (headerParam != null)
                    operation.Operation.Parameters.Remove(headerParam);
            });
            var settings = new SwaggerToCSharpClientGeneratorSettings
            {

All 10 comments

You can set the name of the clients with the ClassName setting:

image

At the moment there is no option to exclude parameters from being generated... You have to first transform the document and remove the unwanted parameters.

@RSuter Thanks (name).
About the authorization header option, is this a feature you would like to have on NSwag?

To understand you correctly: Your swagger spec specifies an authorization header parameter for each operation and you would like to omit this and implement this once in a base class?

Never mind. NSwag is working just fine. There was some code injecting these headers as parameters on the SwaggerUI page, so other developers could test the web services using different credentials. That's why they were appearing on NSwag generated code as well. I commented the code before using the tool and everything is fine now.
Thanks a lot.

To understand you correctly: Your swagger spec specifies an authorization header parameter for each operation and you would like to omit this and implement this once in a base class?

I am after this exact functionality, is this possible somehow?

Some sort of setting to exclude some parameters from being generated?

Yes. I.e. I have a base class for the API client which injects the authorization header, so I don't want to require it on all operations as a parameter.

No, there is no such functionality at the moment. ATM, you need to preprocess the swagger spec before feeding it into the code generator - should be really simple to write a simple command line tool with NSwag.Core to transform the spec...

But maybe we should think about adding a DocumentProcessors preprocessors setting to the code generator settings so that you can transform the spec as part of the generation process...

No, there is no such functionality at the moment. ATM, you need to preprocess the swagger spec before feeding it into the code generator

Thanks, that was easy enough :)

        private static string GenerateApiClientCode(string swaggerJsonContent)
        {
            SwaggerDocument swaggerDoc = SwaggerDocument.FromJsonAsync(swaggerJsonContent).ResultEx();

            //Remove auth header in operations since we supply it in API client base class
            swaggerDoc.Operations.ForEach(operation =>
            {
                SwaggerParameter headerParam = operation.Operation.Parameters.FirstOrDefault((param) =>
                    param.Kind == SwaggerParameterKind.Header &&
                    param.Name == "auth-token");
                if (headerParam != null)
                    operation.Operation.Parameters.Remove(headerParam);
            });
            var settings = new SwaggerToCSharpClientGeneratorSettings
            {

@RSuter any hints on how to exclude a header if I use NSwag.MSBuild?

Was this page helpful?
0 / 5 - 0 ratings