ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
136 stars 64 forks source link

Add CORS related configurations mapping in the generated OpenAPI specification #7321

Open TharmiganK opened 3 weeks ago

TharmiganK commented 3 weeks ago

Current Limitation

In the current Ballerina to OpenAPI specification does not support mapping CORS related configurations in the generated OpenAPI specification.

Example service with CORS configuration:

@http:ServiceConfig {
    cors: {
        allowOrigins: ["http://localhost:3000", "http://localhost"],
        allowCredentials: true,
        allowHeaders: ["X-Username"],
        allowMethods: ["GET", "POST"]
    }
}
service / on new http:Listener(9090) {
    ...

    isolated resource function get winners(@http:Header {name: "X-Username"} string x_username)
          returns Participant[]|error {
        ...
    }

    @http:ResourceConfig {
        cors: {
            allowOrigins: ["http://localhost:3000", "http://localhost"],
            allowCredentials: true,
            allowHeaders: ["X-Username", "Content-Type"],
            allowMethods: ["GET", "POST"]
        }
    }
    isolated resource function post winner(@http:Header {name: "X-Username"} string x_username, 
           Participant payload) returns Participant|error {
        ...
    }
}

Suggested Improvement

The CORS can be mapped with the relevant HTTP Access Control headers and possibly add the preflight request method(OPTIONS) as well.

Version

Ballerina SwanLake Update 10(2201.10.x)