Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation, is it even working ? #653

Open
newportg opened this issue May 10, 2024 · 0 comments
Open

Validation, is it even working ? #653

newportg opened this issue May 10, 2024 · 0 comments

Comments

@newportg
Copy link

I've created a class, below, which has been decorated to set the type and the maximum length.

public class Test
{
    [OpenApiProperty(Nullable = true, Description = "The unique message id of the request")]
    [DataType(DataType.Text)]
    [StringLength(10, MinimumLength = 1, ErrorMessage = "Eeeek")]
    public string ExternalRef { get; set; }
}

In the progam.cs if I set the Openapi version to Version 2

var options = new OpenApiConfigurationOptions()
{
OpenApiVersion = OpenApiVersionType.V2,
}

The Swagger GUI will let me enter a string up to the Maximum length, and pass the call to to the azure function. :)
If I exceed the Maximum length the the Swagger GUI does nothing, no errors or change of colour, nothing. :(

If I update the OpenApiVersion to Version 3
Then every time, whether the string is shorter or longer than the maximum length the call will be passed to the azure function :(

Just for completeness my function is below

    [Function("Function2")]
    [OpenApiOperation(operationId: "Function2")]
    [OpenApiRequestBody(contentType: "application/json", bodyType: typeof(Test), Description = "Test Object", Required = true)]
    [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
    public HttpResponseData Function2([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        using var bodyReader = new StreamReader(req.Body);
        var body = bodyReader.ReadToEndAsync().Result;

        var data = JsonConvert.DeserializeObject<Test>(body);


        var response = req.CreateResponse(HttpStatusCode.OK);
        response.WriteStringAsync("Hello!!! " + data.ExternalRef);
        return response;
    }

This is a Isolated Azure Function V4 .Net 8

What I'd like to know:-
1, How do I set my openapi configuration so that the swagger GUI obeys the schema restrictions.
2, How do I set the openapi configuration so that the swagger GUI will indicate that something is wrong.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant