Open Econest opened 1 year ago
I have the same issue. Is there a way to map some type to others, as we do with SwaggerGenOptions? ie.
services.AddSwaggerGen(c =>
{
c.MapType<IPAddress>(() => new OpenApiSchema { Type = typeof(string).Name });
// ...
});
I've had some luck using the following approach:
Context: I want to customize the representation of DateOnly
objects in the generated schema
Define an IDocumentFilter
to mutate the existing type which has been generated for the DateOnly
object:
public class DateOnlyDocumentFilter : IDocumentFilter
{
public void Apply(IHttpRequestDataObject req, OpenApiDocument document)
{
document.Components.Schemas["dateOnly"] = new()
{
Type = "string",
Format = "date",
};
}
}
Simply add the filter during options setup.
Note that I am using the static approach in which I inherit from DefaultOpenApiConfigurationOptions
public class MyOpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
MyOpenApiConfigurationOptions()
{
DocumentFilters.Add(new DateOnlyDocumentFilter());
}
}
@joan-grau I suspect you can use an approach similar to the one above for your purposes of mutating the representation of the IPAddress
object
The following more generic approach has also proved fruitful:
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
public class CustomTypeDocumentFilter<T> : IDocumentFilter
{
private readonly static string _typeName = typeof(T).GetOpenApiTypeName(new CamelCaseNamingStrategy());
private readonly OpenApiSchema _schema;
public CustomTypeDocumentFilter(OpenApiSchema schema)
{
_schema = schema;
}
public void Apply(IHttpRequestDataObject req, OpenApiDocument document)
{
document.Components.Schemas[_typeName] = _schema;
}
}
public class DateOnlyDocumentFilter : CustomTypeDocumentFilter<DateOnly>
{
public DateOnlyDocumentFilter() : base(new()
{
Type = "string",
Format = "date",
})
{ }
}
Describe the issue We are looking at moving our application to Azure functions v4. We were using an App Service beforehand and swagger works correctly there (we added some configurations for a certain 3rd party library). We used Swashbuckle's MapType function to assert that the type should use the custom schema we define and the schema is then referenced from the containing object correctly. I am not sure of the equivalent functionality here if any.
To Reproduce The project file:
The Schema I add:
The filter to add above schema:
The endpoint and object:
Sample downloadable here: FunctionApp4.zip
Exception (I believe this occurs as it tries to walk through IQuantity properties instead of simply resolving custom schema that was added) System.InvalidOperationException: 'Sequence contains no elements'
Expected behavior Expect document generation to resolve the custom schema when looking at object type of
IQuantity
or a way to tell the generation to do this - in swagger this was done using SwaggerGenOptions:Is there something comparable here?
Screenshots N/A
Environment (please complete the following information, if applicable):
Additional context Think that's everything...