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 ui freeze on nested models. #1398

Closed
bugrakosen opened this issue Nov 30, 2020 · 2 comments
Closed

Swagger ui freeze on nested models. #1398

bugrakosen opened this issue Nov 30, 2020 · 2 comments

Comments

@bugrakosen
Copy link

My DTOs are very nested. When i try expand post and put endpoints the ui freeze like that;

freeze

I've tried multiple solutions like removing nested properties or examples section on model with document and schema filter, installing Swashbuckle.AspNetCore.Newtonsoft package and ignore reference loop handling. Its run normally when i remove the schemas with document filter, but i need schemas in documentation. I've already tried solutions described in anothes issues for this;

  c.DefaultModelExpandDepth(1);
  c.DefaultModelsExpandDepth(1);

Swashbuckle.AspNetCore version : 5.6.3
Swashbuckle.AspNetCore.Newtonsoft version : 5.6.3
.Net Core version : 3.1
Generated JSON documentation file size : 12.0 MB
I included xml file in documentation. Xml file size : 1.3 MB

Thank you in advance for your help.

@muhdammar
Copy link

hi do you fix this error

@bugrakosen
Copy link
Author

Yes, my issue about [FromForm] annotation. If you have IFormFile in your model and you pass parameter with [FromForm] attribute, swashbuckle generate unnecessary codes in docs.json.

I've solved this issue with exclude this endpoints in my docs like below;

[ApiExplorerSettings(IgnoreApi = true)]
public async Task<IActionResult> AddProductAsync([FromForm] ProductDTO product)
{
      ...
      some code
      ...
}

Or;

[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}
public class SwaggerExcludeFilter : ISchemaFilter
{
       public void Apply(OpenApiSchema schema, SchemaFilterContext context)
       {
            if (schema?.Properties == null)
            {
                return;
            }

            var excludedProperties =
                context.Type.GetProperties().Where(
                    t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null);

            foreach (var excludedProperty in excludedProperties)
            {
                var propertyToRemove =
                    schema.Properties.Keys.SingleOrDefault(
                        x => x.ToLower() == excludedProperty.Name.ToLower());

                if (propertyToRemove != null)
                {
                    schema.Properties.Remove(propertyToRemove);
                }
            }
       }
}
public class ProductDTO
{
     [SwaggerExclude]
     public IFormFile Image { get; set; }
}

In swagger configuration;

 services.AddSwaggerGen(options =>
 {
          ...
          options.SchemaFilter<SwaggerExcludeFilter>();
          ...
 });

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