asyncapi / saunter

Saunter is a code-first AsyncAPI documentation generator for dotnet.
https://www.asyncapi.com/
MIT License
204 stars 58 forks source link

Is there pattern support in channel name? #114

Closed AlexanderButs closed 3 years ago

AlexanderButs commented 3 years ago

Hey.

Thanks for such a great project. Looks promising.

Is there a way to describe channel name I publish events to as a pattern like "foo.{some_changing_pattern}"? Some context: I use queues in NATS to publish events and route them to proper websocket server to push to the client further. So I have a lot of queues with different names which follow one naming pattern.

Thanks in advance. Alex.

m-wild commented 3 years ago

Hi Alex, At the moment there are 2 options for describing channel parameters

  1. Easy: Using the [ChannelParameterAttribute]

    [Channel("foo.{id}")]
    [ChannelParameter("id", typeof(int), Description = "The ID of the Foo")]
    public Foo(int id) {}
    channels:
      foo.{id}:
        parameters:
          id:
            description: "The ID of the Foo"
            schema:
              type: number
              format: int32
  2. Advanced: You could also do something like the below to automatically scan for parameters...

    [Channel("foo.{id}")]
    public Foo(int id) {}
    
    public class ParameterFilter : IChannelItemFilter
    {
        public void Apply(ChannelItem channelItem, ChannelItemFilterContext context)
        {
            // here you could grab parameters from the method, or from the channel attribute...
            if (context.Member is MethodInfo method)
            {
                var param = method.GetParameters();
                foreach (var p in param)
                {
                    // add each method parameter to the channel item
                    channelItem.Parameters[p.Name] = new Parameter
                    {
                        Schema = context.SchemaGenerator.Generate(p.GetType())
                    };
                }
            }
        }
    }
    
    services.AddAsyncApiSchemaGeneration(options =>
    {
        options.ChannelItemFilters.Add(new ParameterFilter());
    }
    channels:
      foo.{id}:
        parameters:
          id:
            schema:
              type: number
              format: int32
AlexanderButs commented 3 years ago

Wow! Great! Super extensible library.

Thank you very much!

m-wild commented 3 years ago

Glad to help. I will close this issue now, but feel free to re-open it if you have any problems 😃