Skip to main content

I have been testing an issue with a REST service and came across something that I will post here in case anyone else runs into the issue.

I was using .NET Core 7.0 to create a Web API  that exposes Swagger 2.0  via Swashbuckle and found that no service objects were being created for the Models. I tracked this down to an attribute AdditionalProperties = false being added to the end of the definitions section of the Swagger file. Not sure when this became a thing "if anyone else has seen this keen to hear" as I have not had to worry about this in the past. The code that I added was the following middleware: Add a new class:

public class AdditionalPropertiesSchemaFilter : ISchemaFilter
{
/// <summary>
/// Middleware to allow for additional properties to be true, else no Service Objects will be created by the REST Broker.
/// </summary>
/// <param name="schema"></param>
/// <param name="context"></param>
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsClass)
{
schema.AdditionalPropertiesAllowed = true;
}
}
}

Add the following code to the Program.cs file:

services.AddSwaggerGen(c =>
{
c.SchemaFilter<AdditionalPropertiesSchemaFilter>();
});

With this code added your Object Types will be created as expected. 

Be the first to reply!

Reply