How to hide some properties of a class from swagger

Hi,

I use ServiceStack & ServiceStack.AspNetCore.OpenApi 8.8.0 and Swashbuckle.AspNetCore 9.04 in a NetCore 9 app.

I have the following ConfigureOpenApi : IHostingStartup setup (simplified):

public void Configure(IWebHostBuilder builder)
{
    builder.ConfigureServices((context, services) =>
    {
          services.AddSwaggerGen(options =>
          {
              options.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: false);
          }
          services.AddServiceStackSwagger();
  }

}

The issue I am having is I am unable to hide specific properties of a request class in swagger.json. I need the class to be visible in swagger, just not all properties (but they do need to be serialized).

The request class has [Api], [Tag] and [Route] attributes.

I tried [SwaggerIgnore], [ServiceStack.DataAnnotations.ExcludeMetadata], removing [APIMember], and various SchemaFilter attempts to no avail. I spent hours on this. This shouldn’t be complicated. Maybe I’m too dumb.

Any recommendations?

Thanks.

When using Swagger v3 you’re using Swashbuckle instead of ServiceStack Swagger UIs, so any customization and configuration needs to be done on Swashbuckle.

A quick search of google brought up this thread:

Which suggests you can use [JsonIgnore] or [Obsolete], I’ve had a quick test and appears both work.

Thanks, but none of that works. I see that people have complained on github but nothing was deno. Swashbuckle sucks.

Weird, when I tried both worked for me, i.e. the annotated properties were removed from the Schema in Swagger UI.

I created a repo that shows it is not working as expected.

I’ve just added a change in the latest v8.8.1 pre-release packages which should now ignore properties annotated with [SwaggerIgnore].

That worked–thanks!

I believe the same issue exists for the Restrict() decorator. I am unable to hide PerformanceBase class from swagger.

I tried:

[Restrict(VisibilityTo = RequestAttributes.None)]
[SwaggerSchema(ReadOnly = true)]
[Swashbuckle.AspNetCore.Annotations.SwaggerSchema(ReadOnly = true)]

You should be able to exclude APIs from metadata APIs and pages with [Exclude(Feature.Metadata)].

The problem with that attribute is it also hides the top class.

class TopClass: InheritedClass{}

[Exclude(Feature.Metadata)]
public class InheritedClass{}

Result is TopClass is also hidden from swagger (not the desired result–I only want to hide InheritedClass)

This should only be annotated on API Request DTOs and Request DTOs should not inherit other Request DTOs.