Open Closed

ABP API versioning #2884


User avatar
0
shobhit created
  • ABP Framework version: v4.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

Question: how to version API. i have done below changes but i am not getting swagger working:

1- Controller changes [RemoteService] [Area("app")] [ApiVersion("2")] [ControllerName("BPMMapping")] [ApiExplorerSettings(IgnoreApi = false)] [Route("api/v{version:apiVersion}/app/bpmmappings")] public class BPMMappingV2Controller : AbpController, IBPMMappingAppService { }

2- Host Module Changes

private static void ConfigureSwagger(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddApiVersioning(config => { config.DefaultApiVersion = new ApiVersion(1, 0); config.ReportApiVersions = true; config.AssumeDefaultVersionWhenUnspecified = true; });

        context.Services.AddControllers().AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
        });

        context.Services.AddMvc(options => {
            foreach (var outputFormatter in

options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => .SupportedMediaTypes.Count == 0)) { outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where( => _.SupportedMediaTypes.Count == 0)) { inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        context.Services.AddAbpSwaggerGenWithOAuth(
            configuration["AuthServer:Authority"],
            new Dictionary&lt;string, string&gt;
            {
                {"EzpandCC", "EzpandCC API"}
            },
            options =>
            {
                options.CustomSchemaIds(x => x.FullName);
                options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "EzpandCC API V1", Version = "v1", Description = "API V1", });
                options.SwaggerDoc("v2", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "EzpandCC API V2", Version = "v2", Description = "API V2", });
                options.DocInclusionPredicate((version, desc) =>
                {
                    if (!desc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;
                    var versions = methodInfo.DeclaringType.GetCustomAttributes(true).OfType&lt;ApiVersionAttribute&gt;().SelectMany(attr => attr.Versions);
                    var maps = methodInfo.GetCustomAttributes(true).OfType&lt;MapToApiVersionAttribute&gt;().SelectMany(attr => attr.Versions).ToArray();
                    version = version.Replace("v", "");
                    return versions.Any(v => v.ToString() == version && maps.Any(v => v.ToString() == version));
                });
            });

        context.Services.RegisterServiceIoc();
    }

3- Host Module --> OnApplicationInitialization() changes //----------new start ------------ app.UseSwagger(options => options.RouteTemplate = "swagger/{documentName}/swagger.json"); app.UseSwaggerUI(options => {

            options.DocumentTitle = "Aztute API";
            options.SwaggerEndpoint($"/swagger/v1/swagger.json", $"v1");
            options.SwaggerEndpoint($"/swagger/v2/swagger.json", $"v2");

            IConfiguration configuration = context.GetConfiguration();
            options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
            options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
        });
        app.UseEndpoints(endpoints => endpoints.MapControllers());

		//----------new end ------------

		//--------- default abp start -----------
		
        //app.UseSwagger();
        //app.UseSwaggerUI(options =>
        //{
        //    options.SwaggerEndpoint("/swagger/v1/swagger.json", "EzpandCC API");

        //    IConfiguration configuration = context.GetConfiguration();
        //    options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
        //    options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
        //});

4- Swagger response


4 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, you can check the API Versioning documentation.

    Also, there is an example for API Versioning, you can examine it.

  • User Avatar
    0
    shobhit created

    Hi @Engincan V. i am using 4.2.2 and getting following error:

    NOTE: i am using default application structure as per 4.2.2.

  • User Avatar
    0
    shobhit created

    Hello @EngincanV, My API versioning problem solved as i followed sample and document both. Now challenge is when generating proxy in my angular application. i am getting errors in "api-version" params. please refer below screen shot. what i have to do to make sure that angular generated service proxy class have no issue.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, Service Proxies does not support API versioning for Angular right now. There is an open issue about it. You can follow the issue.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09