The OpenAPI Specification (OAS) is a widely adopted standard for defining RESTful APIs. However, the naming conventions used in OAS often differ from those preferred in Ballerina. To enhance the usability and readability of OAS definitions in Ballerina, it is essential to adapt the OAS naming conventions to align with Ballerina's conventions. This proposal outlines the approach for modifying the OAS to conform to Ballerina naming conventions.
Goals
Convert the OAS with Ballerina-friendly naming conventions to generate clients and services that are aligned with the Ballerina naming conventions.
public type karbon\.Api\.Contacts\.V2\.ClientGroupSummaryDTO record {
string full\-name;
string phone\-number;
}
…
resource isolated function get customer/groups(map<string|string[]> headers = {}, *GetClientQueries queries) returns karbon\.Api\.Contacts\.V2\.ClientGroupSummaryDTO|error {
...
}
}
However, when using this generated record, users may face code readability issues. The escaped characters make the code harder to read and understand. This can lead to confusion and potential mistakes during development.
By addressing this issue we aim to generate Ballerina-friendly code using OAS by applying modifications to the user-given OAS.
Description
The proposed solution is modifying the OAS with the Ballerina-friendly naming conventions for the below-mentioned areas,
Every OAS schema is named under the component sections and parameter names.
Query Parameters and Object property names can not be modified because they are wired to data-binding directly, therefore we need to provide some extensions to the OAS to map the details while data-binding,
Adding extension attribute x-ballerina-name to the OAS query parameters and OAS property names
We are providing two options to the user as a deliverable,
If the user is interested in the returned OAS as a deliverable, we introduce a sub-command to return the modified OAS mentioned above.[2]
$ bal openapi sanitize \<yaml file>
When the user needs to generate a client/service which engages that modified OAS, deliverable is client /service Ballerina code.
When we use the modified OAS according to the Ballerina conventions, we will generate the following Ballerina code as a client.
type KarbonApiContactsV2ClientGroupSummaryDTO record {
@http:Field: {name: "field-name"}
string fullName;
@http:Field: {name: "phone-number" }
string phoneNumber;
}
resource isolated function get customer/groups(map<string|string[]> headers = {}, *GetClientQueries queries) returns KarbonApiContactsV2ClientGroupSummaryDTO|error {
…
}
# Represents the Queries record for the operation
public type GetClientQueries record {
#query annotation for the parameter
@http:Query{ name: "customer-group" }
string? customerGroup?;
//other remaining query parameters
};
Here mentioned @http:Field is used for data binding with actual payload, the proposal can be found here. This annotation value will populate using the extensions we have mentioned in the above extension details section. This annotation allows users to define custom names for fields when they are serialized to OAS.
Summary
The OpenAPI Specification (OAS) is a widely adopted standard for defining RESTful APIs. However, the naming conventions used in OAS often differ from those preferred in Ballerina. To enhance the usability and readability of OAS definitions in Ballerina, it is essential to adapt the OAS naming conventions to align with Ballerina's conventions. This proposal outlines the approach for modifying the OAS to conform to Ballerina naming conventions.
Goals
Motivation
[1] Referring to the below OAS sample:
karbon.Api.Contacts.V2.ClientGroupSummaryDTO
is hard to understand and doesn't follow Ballerina's naming rules.full-name
, andphone-number
in the object, which also don't match Ballerina's naming style.Generated Ballerina Code:
However, when using this generated record, users may face code readability issues. The escaped characters make the code harder to read and understand. This can lead to confusion and potential mistakes during development. By addressing this issue we aim to generate Ballerina-friendly code using OAS by applying modifications to the user-given OAS.
Description
The proposed solution is modifying the OAS with the Ballerina-friendly naming conventions for the below-mentioned areas,
x-ballerina-name
to the OAS query parameters and OAS property namesWe are providing two options to the user as a deliverable,
@http:Field
is used for data binding with actual payload, the proposal can be found here. This annotation value will populate using the extensions we have mentioned in the above extension details section. This annotation allows users to define custom names for fields when they are serialized to OAS.@http:Query
annotation is used to capture the query parameter name modification in client/service generation Annotation implementation more details can be found here : https://github.com/ballerina-platform/ballerina-library/issues/6747