ITADN

[Question]: How to add example for something like DateTime / DateOnly?

#505ClosedTsingis 创建于 2025-08-30
question
T
Tsingiscommented
### What do you want to achieve? I achieved this with APIWeaver.OpenApi like [this](https://github.com/Tsingis/bitcoin-web-api/blob/95d20369ba831ac2f4b2258af12dea5dd52446df/src/Api/Setup/ApiServices.cs#L45) and I would like to away from it as it seems to be no longer maintained. Running into issues finding openapi document from `/openapi/v1.json` now `InvalidOperationException: No JsonSerializerContext has been configured on the OpenApiExtensionsOptions instance for the OpenAPI document "v1".` ### What code or approach do you have so far? Currently just quick experimenting from the README using ```csharp using MartinCostello.OpenApi; namespace Api.Setup; public sealed class DateOnlyExampleProvider : IExampleProvider<DateOnly> { public static DateOnly GenerateExample() => DateOnly.FromDateTime(DateTime.UtcNow); } ``` ```csharp services.AddHttpContextAccessor(); services.AddOpenApiExtensions(opt => { opt.AddServerUrls = true; opt.AddExamples = true; opt.AddExample<DateOnly, DateOnlyExampleProvider>(); }); ``` ### Additional context Full code [here](https://github.com/Tsingis/bitcoin-web-api/pull/197)
关闭于 2025-08-30 2 条评论