Skip to content

How to Access operation Tags from CodeGeneration CSharp templates? #3607

@SerialRYaya

Description

@SerialRYaya

Hello!

I have a server which exposes its api definition with swagger.
Some of the webmethods are tagged and the tags are in the definitions.
I would like to generate a client for this server with specific logic related to the operation tags.

The OpenApi definition looks like this:
{ "openapi": "3.0.1", "info": { "title": "MyServer", "version": "v1" }, "paths": { "/MyTaggedOperation": { "get": { "tags": [ "MyTag" ],[...]
I would like to access the tags from the templates so I can apply a specific logic in the generated client class.

From the NSwag source code, master branch, I found a way to do it by adding an accessor on the operation tags in the NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel:
/// <summary>Gets the tags associated with this operation.</summary> public List<string> Tags { get { return _operation.Tags; } }
Then, from the Client.Class.Liquid template, I can add the following:

{% for operation in Operations -%} {% if operation.Tags contains "MyTag" -%} // the operation is tagged "MyTag", do something related {% else %} // do something else {% endif %} {% endfor %}

My issue is that I had to modify NSwag source code to make this work but I would like to avoid maintaining a fork if there's a better way.

Is this use case already taken into account? Is there and what is the way to access operation tags from the csharp client generator templates (or any other generator templates since NSwag is not exclusive to CSharp) please?

If not, what's the way to request this feature please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions