RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.82k stars 1.3k forks source link

How to hide hide model properties when using modelbinder in aspnet core 3? #2539

Open bergziege opened 5 years ago

bergziege commented 5 years ago

Hi all, we have some web apis that recieve a guid from the url and model bind it to an entity. Until aspnet core 2.1 we could use a type mapper as follows:

swaggerUiOwinSettings.TypeMappers.Add(new PrimitiveTypeMapper(entityType, s => s.Type = JsonObjectType.String));
app.UseSwaggerUi(typeof(Startup).Assembly, swaggerUiOwinSettings);

Which just shows the route parameter but hides all properties from the mapped type.

Recently we startet a new aspnet core 3 web api and tried the same procedure as follows:

services.AddOpenApiDocument(config => {
  config.TypeMappers.Add(new PrimitiveTypeMapper(typeof(Wafer), delegate (JsonSchema s) {
      s.Type = JsonObjectType.String;
      s.Title = "guid";
      s.Description = "BusinessId";
  }));
});

I now correctly get a BusinessId property but also every property from the mapped 'Wafer' type which where hidden when using type mappers in core 2.1.

[HttpPatch]
[Route("{wafer:guid}")]
public IActionResult Patch([ModelBinder, FromRoute] Wafer wafer, [FromBody] dynamic patchData) {

So how do I hide the mapped model properties in the UI when using aspnet core 3?

grafik

RicoSuter commented 4 years ago

Use the [OpenApiIgnore] attribute to hide a property.