Closed lnash94 closed 1 week ago
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 theName
annotation for both the record fields and parameters. Shall we use the same here
# 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 openapi:Name
annotation if we only need the name for mapping. But the proposal also includes format
. @lnash94 Can you elaborate the requirement for this?
We need to have a section on how are we going to map this with the data-binding features provided by the Ballerina http
package.
@daneshk, @TharmiganK Following are some points to have two separate annotations for data binding
An optional format
field to the record field to capture whether the format is XML or JSON when performing data binding. This field requires discussion with the HTTP module regarding data binding (we can remove this field if the record fields always support JSON data binding).
Upon further observation, the parameter may have specific fields beyond the record fields, such as parameter serialization options. Refer to Swagger's serialization documentation for more details. Currently, we have no way to represent these serialization details in query parameters. Therefore, introducing a specific annotation for parameters could allow room for future expansion of these details.
type ParamAnnotation record {|
string name;
string explode;
string style;
|}
Attendees Meeting: @shafreenAnfar , @daneshk , @lnash94 , @TharmiganK Proposal Meeting Notes:
@http:Query
annotation for query parameter details mapping Attendees : @daneshk , @lnash94 , @TharmiganK Annotation Name finalise
@http:Field
as the annotation name consistent with the query annotation@openapi:Query
as a separate annotation for handling the OpenAPI tool's client generation. Since we have designed the query parameters to be passed as included record parameters in the function, they will be represented as record fields to the client connector. We need this annotation solely to concatenate the name, and we've decided to implement it on the OpenAPI tool side.2024/09/02 : @http:Query
Annotations support for record fields
Attendees: @daneshk , @TharmiganK , @lnash94
@http:Query
for native HTTP client , we observed that we can not attach attachment points into functions arguments, therefore we specialized this client query parameter support via the record parameter included type.Implement HTTP client-related task :
toJson
method https://github.com/ballerina-platform/module-ballerina-http/pull/2204We can close this proposal since we provided all fixes and tested the change against the depended ballerina packages[1] [1] https://github.com/ballerina-platform/module-ballerina-websub/pull/666 https://github.com/ballerina-platform/module-ballerina-websocket/pull/1462 https://github.com/ballerina-platform/module-ballerina-grpc/pull/1653 https://github.com/ballerina-platform/module-ballerina-websubhub/pull/1039 https://github.com/ballerina-platform/module-ballerina-graphql/pull/2088 https://github.com/ballerina-platform/module-ballerina-sql/pull/748 https://github.com/ballerina-platform/module-ballerinax-kafka/pull/1214 https://github.com/ballerina-platform/module-ballerinax-rabbitmq/pull/1001
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 https://github.com/ballerina-platform/ballerina-library/issues/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 wayHowever, 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,
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 hereAnnotation using in-record field
type Person record { @http:Field {| name: "first-name"|} string firstName; int age; }
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