fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.29k stars 1.58k forks source link

Swagger : Failed to load API definition #968

Closed Mike6x closed 4 months ago

Mike6x commented 4 months ago

I try to add a simple module like Todo, but have error with swagger like: image

PLS HELP !

Flolowing is my DimensionModule.cs file:

using Carter;
using FSH.Framework.Core.Persistence;
using FSH.Framework.Infrastructure.Persistence;
using FSH.Starter.WebApi.Setting.Domain;
using FSH.Starter.WebApi.Setting.Features.v1;
using FSH.Starter.WebApi.Setting.Persistence;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace FSH.Starter.WebApi.Setting;
public static class DimensionModule
{

    public class Endpoints : CarterModule
    {
        public Endpoints() : base("setting") { }
        public override void AddRoutes(IEndpointRouteBuilder app)
        {
            var settingGroup = app.MapGroup("dimensions").WithTags("dimensions");
            settingGroup.MapDimensionCreationEndpoint();
            settingGroup.MapGetDimensionEndpoint();
            settingGroup.MapGetDimensionListEndpoint();
            settingGroup.MapDimensionUpdationEndpoint();
            settingGroup.MapDimensionDeletionEndpoint();
        }
    }
    public static WebApplicationBuilder RegisterDimensionServices(this WebApplicationBuilder builder)
    {
        ArgumentNullException.ThrowIfNull(builder);
        builder.Services.BindDbContext<DimensionDbContext>();
        builder.Services.AddScoped<IDbInitializer, DimensionDbInitializer>();
        builder.Services.AddKeyedScoped<IRepository<Dimension>, DimensionRepository<Dimension>>("setting:dimension");
        builder.Services.AddKeyedScoped<IReadRepository<Dimension>, DimensionRepository<Dimension>>("setting:dimension");
        return builder;
    }
    public static WebApplication UseDimensionModule(this WebApplication app)
    {
        return app;
    }
}

Extensions.cs

using System.Reflection;
using Asp.Versioning.Conventions;
using Carter;
using FluentValidation;
using FSH.Starter.WebApi.Catalog.Application;
using FSH.Starter.WebApi.Catalog.Infrastructure;
using FSH.Starter.WebApi.Setting;
using FSH.Starter.WebApi.Todo;

namespace FSH.Starter.WebApi.Host;

public static class Extensions
{
    public static WebApplicationBuilder RegisterModules(this WebApplicationBuilder builder)
    {
        ArgumentNullException.ThrowIfNull(builder);

        //define module assemblies
        var assemblies = new Assembly[]
        {
            typeof(CatalogMetadata).Assembly,
            typeof(TodoModule).Assembly,
            typeof(DimensionModule).Assembly
        };

        //register validators
        builder.Services.AddValidatorsFromAssemblies(assemblies);

        //register mediatr
        builder.Services.AddMediatR(cfg =>
        {
            cfg.RegisterServicesFromAssemblies(assemblies);
        });

        //register module services
        builder.RegisterCatalogServices();
        builder.RegisterTodoServices();
        builder.RegisterDimensionServices();

        //add carter endpoint modules
        builder.Services.AddCarter(configurator: config =>
        {
            config.WithModule<CatalogModule.Endpoints>();
            config.WithModule<TodoModule.Endpoints>();
            config.WithModule<DimensionModule.Endpoints>();
        });

        return builder;
    }

    public static WebApplication UseModules(this WebApplication app)
    {
        ArgumentNullException.ThrowIfNull(app);

        //register modules
        app.UseCatalogModule();
        app.UseTodoModule();
        app.UseDimensionModule();

        //register api versions
        var versions = app.NewApiVersionSet()
                    .HasApiVersion(1)
                    .HasApiVersion(2)
                    .ReportApiVersions()
                    .Build();

        //map versioned endpoint
        var endpoints = app.MapGroup("api/v{version:apiVersion}").WithApiVersionSet(versions);

        //use carter
        endpoints.MapCarter();

        return app;
    }
}
iammukeshm commented 4 months ago

Looks fine to me. Maybe there is an issue with your minimal endpoint? Is your repo available publicly?

Mike6x commented 4 months ago

Dear @iammukeshm,

My repo is public : https://github.com/Mike6x/dotnet-starter-kit/tree/v2dev

Please help me !

iammukeshm commented 4 months ago
property: DefaultValue(null)] Guid? FatherId

this was the code that created the issue for you. I changed it to

Guid? FatherId

It worked fine.

image

Mike6x commented 4 months ago

It worrked. Thank for your support.