Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work without AddService for examples provider #124

Open
wizofaus opened this issue Dec 5, 2019 · 5 comments
Open

Doesn't work without AddService for examples provider #124

wizofaus opened this issue Dec 5, 2019 · 5 comments

Comments

@wizofaus
Copy link

wizofaus commented Dec 5, 2019

"Then all you need to do is implement IExamplesProvider...and that's it"

Really? Not only can I not get this to work as described, but looking at the source code I can't see any way it could ever work, as the operation filter requires that your examples provider be registered with the service provider.
I did manage to get it to work using
services.AddSingleton<IExamplesProvider<FooBarRequest>,FooBarRequestExampleProvider>();

in my app Startup, but I don't see where it says you need this?

@mattfrear
Copy link
Owner

mattfrear commented Dec 5, 2019

Have a re-read of the Installation section.
https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters#installation

If you want to use the Request and Response example filters (and have called c.ExampleFilters() above), then you MUST also call

services.AddSwaggerExamplesFromAssemblyOf<MyExample>();

@wizofaus
Copy link
Author

wizofaus commented Dec 5, 2019

But all the classes are in the same assembly as the Startup class, so why wouldn't services.ExampleFilters() be sufficient?
(But yes, on re-reading it does say that, I just don't quite understand why ExampleFilters() can't use Assembly.GetCallingAssembly() as the default assembly for where the example providers can be found. Also I'm not convinced "ExampleFilters" is the best name for a function - what's the verb?)

@mattfrear
Copy link
Owner

mattfrear commented Dec 5, 2019

Swashbuckle enables filters by calling c.OperationFilter<T>() method.
I guess I was copying that naming pattern when I came up with the c.ExampleFilters() method name.

I just don't quite understand why ExampleFilters() can't use Assembly.GetCallingAssembly() as the default assembly for where the example providers can be found.

Because I hadn't though of doing that 😀 Sounds like a good idea, and it would make the installation of the examples filter easier. We could leave the AddSwaggerExamplesFromAssemblyOf<T> method there in case users had the examples in another assembly.

@wizofaus
Copy link
Author

wizofaus commented Dec 6, 2019

NP, and BTW thanks for putting this on github, will definitely be using it!

@mattfrear
Copy link
Owner

I just don't quite understand why ExampleFilters() can't use Assembly.GetCallingAssembly() as the default assembly for where the example providers can be found

I just had a go at implementing this, by changing my ExampleFilters extension method to take a service collection:

        public static void ExampleFilters(this SwaggerGenOptions swaggerGenOptions, IServiceCollection services)
        {
            services.AddSwaggerExamplesFromAssemblies(Assembly.GetExecutingAssembly());

            ExampleFilters(swaggerGenOptions);
        }

which can be called via

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v2" });

                options.ExampleFilters(services);

It looks like it should work, I can see the Examples get registered with the ServiceCollection... but when I run my test project, it falls over at runtime 😥:

InvalidOperationException: Unable to resolve service for type 'Swashbuckle.AspNetCore.Filters.RequestExample' while attempting to activate 'Swashbuckle.AspNetCore.Filters.ExamplesOperationFilter'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants