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

[Step 1] Allow constraint validation for path, query and header parameters #4371

Closed
12 tasks done
TharmiganK opened this issue Apr 21, 2023 · 1 comment · Fixed by ballerina-platform/module-ballerina-http#1608
Assignees
Labels
module/http Points/2 Team/PCM Protocol connector packages related issues Type/Task

Comments

@TharmiganK
Copy link
Contributor

TharmiganK commented Apr 21, 2023

Description:

Implementing the first step of Add constraint support for path, query and header params #3239

This task is to implement automatic constraint validations for path, query and header parameters if the parameter type is defined with constraints.

Describe your task(s)

The following use cases should be verified:

  • Path parameter type

    @constraint:Int { minValue: 1, maxValue: 100 }
    type Id int;
    
    resource function get users/[Id id]() returns User {
        ...
    }
  • Rest path parameter type

    @constraint:String { pattern: re`[a-zA-Z]+` }
    type Path string;
    
    resource function get [Path ...path]() returns http:Response {
        ...
    }
  • Query parameter basic type

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(UserName name) returns user {
        ...
    }
  • Query parameter basic type optional

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(UserName? name) returns User {
        ...
    }
  • Query parameter basic type default negative

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(UserName name = "a") returns User {
        ...
    }
  • Query parameter basic array type

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    @constraint:Array { minLength: 1, maxLength: 5}
    type UserNames UserName[];
    
    resource function get users(UserNames names) returns User[] {
        ...
    }
  • Query parameter map type

    @constraint:Int { minValue: 18, maxValue: 100}
    type Age int;
    
    type UserDetails record {|
        @constraint:String { minLength: 2, maxLength: 20 }
        string name;
        Age age;
    |};
    
    resource function get users(UserDetails details) returns User {
        ...
    }
  • Header parameter basic type

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(@http:Header UserName x\-name) returns User {
        ...
    }
  • Header parameter basic type optional

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(@http:Header UserName? x\-name) returns User {
        ...
    }
  • Header parameter basic type default negative

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    resource function get users(@http:Header UserName x\-name = "a") returns User {
        ...
    }
  • Header parameter basic array type

    @constraint:String { minLength: 2, maxLength: 20 }
    type UserName string;
    
    @constraint:Array { minLength: 1, maxLength: 5}
    type UserNames UserName[];
    
    resource function get users(@http:Header UserNames x\-names) returns User[] {
        ...
    }
  • Header parameter map type

    @constraint:Int { minValue: 18, maxValue: 100}
    type Age int;
    
    type UserDetails record {|
        @constraint:String { minLength: 2, maxLength: 20 }
        string x\-name?;
        @constraint:Array { maxLength : 2 }
        Age[] x\-ages?;
    |};
    
    resource function get users(@http:Header UserDetails details) returns User|User[] {
        ...
    }
@TharmiganK
Copy link
Contributor Author

With the above PR, all the constraint validation errors (including path parameter) are returned as 400 - Bad Request responses. Initially, it was planned to give a 404 - Not Found response for constraint validation failure in the path parameter but later we have decided to give the same response to be consistent. Will think about this later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/http Points/2 Team/PCM Protocol connector packages related issues Type/Task
Projects
Archived in project
1 participant