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

Swagger - MapVogenTypes not working with class libraries #691

Closed
mkalinski93 opened this issue Oct 26, 2024 · 3 comments
Closed

Swagger - MapVogenTypes not working with class libraries #691

mkalinski93 opened this issue Oct 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@mkalinski93
Copy link

mkalinski93 commented Oct 26, 2024

Describe the bug

I have a multi repo project that I´m working on and I was using the MapVogenTypes extension method for SwaggerGen. It seems to me, that it can only find ValueObjects in the same Assembly.

Steps to reproduce

  1. Create a plain WebApplication (.NET 8)
  2. Add Vogen via Nuget
  3. Create a simple ValubOject
  4. Use the assembly instructions with "GenerateSwashbuckleMappingExtensionMethod" and add MapVogenTypes.

This case works. Here comes the issue

  1. Create a class library
  2. Add another simple ValueObject inside the class library
  3. Reference the WebApplication project to the ClassLibrary.

MapVogenTypes will not resolve the ValueObject that remains in the Class library.

Expected behaviour

Types should have been mapped

@mkalinski93 mkalinski93 added the bug Something isn't working label Oct 26, 2024
@SteveDunn
Copy link
Owner

Thank you for the bug report @mkalinski93 . Apologies for that. I'll take a look shortly!

@SteveDunn
Copy link
Owner

Unfortunately, source generators are per-project, so it can't inspect any types found in other projects.
A workaround would be to generate the same extension method in your library. But unfortunately, it writes this method without a namespace. So, without an import alias when you reference the project, you won't be able to differentiate.

To make this easier, I've made it generate the default namespace of the project. If you have types that need mapping and they're in different projects, then you'll need to change the registration:

    //opt.MapVogenTypes(); old way -for unambiguous extension method

    // when you have multiple projects with types that need mapping:
    WebApplication.VogenSwashbuckleExtensions.MapVogenTypes(opt);
    WebApplication.Shared.VogenSwashbuckleExtensions.MapVogenTypes(opt);

I've update the examples to show this too. In your library with value objects, add package Swashbuckle.AspNetCore and an assembly attribute:

[assembly: VogenDefaults(
    openApiSchemaCustomizations: OpenApiSchemaCustomizations.GenerateSwashbuckleSchemaFilter | OpenApiSchemaCustomizations.GenerateSwashbuckleMappingExtensionMethod,
    staticAbstractsGeneration: StaticAbstractsGeneration.MostCommon | StaticAbstractsGeneration.InstanceMethodsAndProperties]

This is currently in a branch and I'll release as part of the next beta soon.

Thanks again for the report

@SteveDunn
Copy link
Owner

In 5.0.5-beta3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants