Nswag: Use of DI for internal types breaks Lamar

Created on 6 Dec 2018  路  6Comments  路  Source: RicoSuter/NSwag

I recently found an issue with NSwag.AspNetCore and the Lamar IoC library (successor to the venerable StrutureMap), where a call to UseSwagger causes an exception.

Full details are available here.

The issue is caused by NSwag.AspNetCore's unusual use of DI to resolve internal types, which can be seen here:

var documents = app.ApplicationServices.GetRequiredService<IEnumerable<SwaggerDocumentRegistration>>();

While it could be argued that this is Lamar's problem, I'm of the opinion that using DI to resolve internal types is an anti-pattern.

This can be resolved in a couple of ways:

  1. Just mark SwaggerDocumentRegistration as public
  2. NSwag seems to use DI for SwaggerDocumentRegistration as more of a 'storage mechanism', than actual DI (since registrations are added in ConfigureServices using AddSwaggerDocument, so an alternative would be to not add these to the container, but use some kind of storage class instead

Option 2 obviously changes the public API, whereas option 1 involves changing a single keyword and shouldn't break anything, so option 1 seems like the obvious way to resolve this.

I'm happy to make this change and test it, if you're willing to accept a PR for this?

help wanted bug

All 6 comments

I think it's fine to just change it to public

Tasks:

  • Make SwaggerDocumentRegistration public
  • Add xml docs + file header

Excellent, I'll send a PR for this, thanks for being decisive!

Sorry, I did it myself so I can release this now too :)

Even better :)

I'll test the new Nuget package with Lamar on Monday!

Have confirmed this fix allows NSwag to work with Lamar

Was this page helpful?
0 / 5 - 0 ratings