asyncapi / saunter

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

Saunter breaks when you try register two same channels #133

Closed alan994 closed 2 years ago

alan994 commented 2 years ago

I have a simple scenario where in the same application I have publishing and subscribing code. Both operations are targeting the same topic (channel). When trying to generate a document I get an error.

System.ArgumentException:
 An item with the same key has already been added.
  Key: conversation.user\r\n   
  at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\r\n
   Saunter.Generation.DocumentGenerator.GenerateChannelsFromMethods(IEnumerable`1 asyncApiTypes, AsyncApiSchemaResolver schemaResolver, AsyncApiOptions options, JsonSchemaGenerator jsonSchemaGenerator, IServiceProvider serviceProvider)\r\n
   Saunter.Generation.DocumentGenerator.GenerateChannels(TypeInfo[] asyncApiTypes, AsyncApiSchemaResolver schemaResolver, AsyncApiOptions options, JsonSchemaGenerator jsonSchemaGenerator, IServiceProvider serviceProvider)\r\n
   Saunter.Generation.DocumentGenerator.GenerateDocument(TypeInfo[] asyncApiTypes, AsyncApiOptions options, AsyncApiDocument prototype, IServiceProvider serviceProvider)\r\n
   Saunter.Generation.AsyncApiDocumentProvider.GetDocument(AsyncApiOptions options, AsyncApiDocument prototype)\r\n
   Saunter.AsyncApiMiddleware.Invoke(HttpContext context)\r\n
   Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n
   Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware.InvokeInternal(HttpContext context)\r\n
....

The problem is here. Underlaying collection is IDictionary and, if I understood correctly, it is not possible to have the same channels declared multiple times in the same application. Are there any plans to support this scenario?

alan994 commented 2 years ago

@tehmantra is this project still active?

m-wild commented 2 years ago

@alan994 sorry have been on Xmas holidays. I Will get to this in the coming week 🙂

alan994 commented 2 years ago

If I can help somehow, let me know. Tnx.

rkpin commented 2 years ago

we have a couple of scenarios where we tried to register more than one operation with the same channel.

  1. app publishing and subscribing from the same channel/ topic.
  2. app with multiple subscriptions from same channel/ topic.

saunter breaks if you have multiple channel attributes with the same name. AsychAPI 2.3 spec supports more than one subscription and publishers under a single channel. It would be great if saunter also supports this.

alan994 commented 2 years ago

@tehmantra Did you have a chance to look at this? Any open questions around this issue?

cmcjcharters commented 2 years ago

I have also encountered this issue, and am happy to propose a PR if I can get something to work. We also have a case where we have pub/sub on the same channel (Websockets)

Looking at the source code, it looks like Channel can be declared as an attribute of a a class, but if it is, there is an expectation that the PublishOperation and SubscribeOperation are also class-level, not methods within that class. And of course, as we have seen, if we use Operation and PublishOperation / SubscribeOperation at the method level within a class, we can't use Operation with the same name more than once due to the underlying data structure being a dictionary.

MohamadTahir commented 2 years ago

Any progress on this?

m-wild commented 2 years ago

Fixed in v0.10.0 https://www.nuget.org/packages/Saunter/0.10.0