Azure / Azure/azure-functions-openapi-extension

Validation based on OpenApiExampleAttribute?

Open
#577 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
388
Forks
202
PR merge metrics
No merged PRs in 30d

Description

The OpenAPI extension lets you define how a body should look like. ([OpenApiExampleAttribue](https://github.com/Azure/azure-functions-openapi-extension/blob/main/docs/openapi-core.md#openapiexampleattribute))

This could look like that:

```csharp
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace DiamAPI.Models;

[OpenApiExample(typeof(CatExample))]
public class Cat
{
public int Id { get; set; }

[OpenApiProperty(Nullable = false, Default = "Nabi", Description = "The name your cat does not listen to.")]
public string Name { get; set; }

public CatBreed Breed { get; set; }
}

[JsonConverter(typeof(StringEnumConverter))]
public enum CatBreed
{
[Display("Siamese")]
Siamese = 0,

[Display("Maine Coon")]
MaineCoon = 1,

[Display("Persian")]
Persian = 2,
}

public class CatExample : OpenApiExample
{
public override IOpenApiExample Build(NamingStrategy namingStrategy = null)
{
Examples.Add(OpenApiExampleResolver.Resolve("nabi", new Cat() { Id = 123, Name = "Nabi", Breed = CatBreed.Siamese }, namingStrategy));

return this;
}
}
```

Is there any way to validate the body against this schema? Right now this only serves to have a nice UI and nothing more, but I hope I overlooked something.

![image](https://github.com/Azure/azure-functions-openapi-extension/assets/2971735/257d1bb8-781f-49ec-a2a8-7a9ea89031dc)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.