-
Notifications
You must be signed in to change notification settings - Fork 64
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
[Proposal]: Data binding annotations for custom and actual names in Ballerina fields and resource/remote parameters #6747
Comments
How about introducing a single annotation to represent the mapping between OpenAPI and Ballerina? Annotation definition: public type MappingInfo record {|
string name;
|};
public const annotation MappingInfo Mapping on record field, parameter; Sample usage: type Person record {|
@openapi:Mapping { name: "full-name" }
string fullName;
@openapi:Mapping { name: "phone-number" }
string phoneNumber;
|}; |
+1 for having a single annotation. In xmldata, persist, we used the # Defines the schema name which matches the record field. The default value is the record field name.
public type NameConfig record {|
# The schema fields and parameters value
string value;
|};
# The Annotation used to map schema fields and param to record and record fields
public annotation NameConfig Name on record field, record; |
+1 for the We need to have a section on how are we going to map this with the data-binding features provided by the Ballerina |
@daneshk, @TharmiganK Following are some points to have two separate annotations for data binding
type ParamAnnotation record {|
string name;
string explode;
string style;
|}
|
Attendees Meeting: @shafreenAnfar , @daneshk , @lnash94 , @TharmiganK
Attendees : @daneshk , @lnash94 , @TharmiganK
|
2024/09/02 : Attendees: @daneshk , @TharmiganK , @lnash94
|
Implement HTTP client-related task :
|
We can close this proposal since we provided all fixes and tested the change against the depended ballerina packages[1] |
Updated with review : 2024/09/02
Summary
This proposal suggests adding annotation features to improve Ballerina’s usability and compatibility with modern web standards. Seamless JSON serialization and deserialization are crucial for its adoption in modern software development, especially for web APIs and services.
Goals
Motivation
[1] Referring to the OAS sample, since this annotation is required to be part of this implementation #6867
In the above OAS example, it has REST API
get
with query parameter name,customer-group
which is not aligned with Ballerina naming conventions. when we writing the ballerina code we were supposed to use escape for the query parameter name. ex: customer\-group[2] Referring to the JSON sample
Given JSON sample[2] has fields with names "full-name", "last-name" and "table", which are not aligned with Ballerina naming conventions. In addition to that some scenarios can have a ballerina reserved key as a field name like
table
.Therefore if the user needs to use this JSON in the Ballerina record we have to escape special characters like the below way
However, when using this generated record and the query parameters, users may face code readability issues. The escaped characters can make the code harder to read and understand, leading to confusion and potential mistakes during development.
Description
Through this proposal, we are suggesting having an annotation for users to override the record field name according to the preferred name.
Proposed designed
Annotation field for parameters
We propose introducing a new annotating field
@http:Query
parameters with metadata. The field will be specified using a syntax similar to the following:This HttpQuery annotation has one field for storing that parameter name.
Annotation for record fields
We propose introducing a new syntax for annotating record fields with JSON metadata. The annotations will be specified using a syntax similar to the following:
This RecordField annotation enriches
name
for the given record field name. The fieldformat
for the media for serialization which is optional can be added later.This suggested approach will,
ex: Here
table
is a reserved wordUser experience with proposed annotations (@http:Query , @http:Field):
[3] User experience for HTTP service type
[4] User experience for HTTP service implementation
[5] User experience for HTTP client
[5.1] HTTP client with resource function
Currently, there is no way to define the attachment point for function arguments. Therefore, we decided to have the user provide a record parameter included in the query parameters and enable
@http:Query
within the record fields to capture query parameter-related meta information.[5.2] HTTP client with remote function
Annotation usage for OpenAPI Tool generated client and service
The OpenAPI tool encountered an issue while generating Ballerina service and client code for a given OAS reference proposal.
Service code generation: the tool included the
@http:Query
and@http:Field
annotations in the generated service, as described here[3].Service contract type generation: the tool included the
@http:Query
and@http:Field
annotations in the generated service contract type, as described hereHowever, when generating the client, the tool observed an issue where the generated client treated the query parameters as included record parameters. Therefore we need to enable
@http:Query
annotation within those record fields.The rest of the payload, and response data-binding used
@http:Field
annotations for record fields.User experience with generated code
Annotation usage for OpenAPI Tool generated OpenAPI Contracts for particular service
The given
@http:Field
and@http:Query
annotation names are mapped under thex-ballerina-name
extension in the particular sections.Given ballerina service with annotations
Expected generated OpenAPI contract
The text was updated successfully, but these errors were encountered: