Skip to content

swaggergen not using custom json converter #2500

@trampster

Description

@trampster

We are using mongodb which has an ObjectId type (struct). We use a JsonConverter to serialize this as a string, This converter is registered using builder.Services.AddJsonOptions. It works perfectly in our actual API.

However when the swagger is generated the examples have it serialised as an object:

"id": {
        "timestamp": 0,
        "creation_time": "2022-09-13T22:36:23.872Z"
      }

Things we have tried which didn't work:

  • using an example tag in the xml comments in the model (this works for other properties but not this one)
    /// <summary>
    /// </summary>
    /// <example>123456456987</example>
    public ObjectId Id { get; set; }
  • using a ISchemaFilter to change the type and set the example (code gets hit but has no effect on example)
public class SwaggerDiversionFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        foreach(var property in schema.Properties)
        {
            if(context.Type == typeof(Diversion))
            {
                if(property.Key == "id")
                {
                    property.Value.Example = new OpenApiString(new ObjectId().ToString());
                    property.Value.Type = "string";
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help-wantedA change up for grabs for contributions from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions