From 18ded0c10ad814886cdfbceaae49f2edc0ccaef9 Mon Sep 17 00:00:00 2001 From: lnash94 Date: Fri, 6 Dec 2024 17:04:19 +0530 Subject: [PATCH] Enable resource config annotation for service contract type --- module-ballerina-openapi/annotation.bal | 2 +- .../openapi/cmd/BallerinaToOpenAPITests.java | 7 ++ .../Ballerina.toml | 4 + .../result.yaml | 83 +++++++++++++++++++ .../service_file.bal | 58 +++++++++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/Ballerina.toml create mode 100644 openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/result.yaml create mode 100644 openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/service_file.bal diff --git a/module-ballerina-openapi/annotation.bal b/module-ballerina-openapi/annotation.bal index a68f5a62b..bbec61d0f 100644 --- a/module-ballerina-openapi/annotation.bal +++ b/module-ballerina-openapi/annotation.bal @@ -100,7 +100,7 @@ public type Examples readonly & record {| |}; # Annotation for additional OpenAPI information of a Ballerina resource function. -public const annotation ResourceInformation ResourceInfo on object function; +public const annotation ResourceInformation ResourceInfo on object function, object field; # Annotation for additional OpenAPI information of a Ballerina service. public annotation ServiceInformation ServiceInfo on service, type; diff --git a/openapi-integration-tests/src/test/java/io/ballerina/openapi/cmd/BallerinaToOpenAPITests.java b/openapi-integration-tests/src/test/java/io/ballerina/openapi/cmd/BallerinaToOpenAPITests.java index c915afa38..0c4786e53 100644 --- a/openapi-integration-tests/src/test/java/io/ballerina/openapi/cmd/BallerinaToOpenAPITests.java +++ b/openapi-integration-tests/src/test/java/io/ballerina/openapi/cmd/BallerinaToOpenAPITests.java @@ -209,6 +209,13 @@ public void openapiServiceConfigForServiceContractType() throws IOException, Int "project_openapi_info_with_service_contract_type/result.yaml", true); } + @Test(description = "Generate openapi for service contract type with resourceInfo annotation") + public void openapiResourceConfigForServiceContractType() throws IOException, InterruptedException { + executeCommand("project_resource_info_with_service_contract_type/service_file.bal", + "v1_openapi.yaml", + "project_resource_info_with_service_contract_type/result.yaml", true); + } + @AfterClass public void cleanUp() throws IOException { TestUtil.cleanDistribution(); diff --git a/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/Ballerina.toml b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/Ballerina.toml new file mode 100644 index 000000000..496825475 --- /dev/null +++ b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org= "ballerina" +name= "service_contract" +version= "2.0.0" diff --git a/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/result.yaml b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/result.yaml new file mode 100644 index 000000000..449a62736 --- /dev/null +++ b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/result.yaml @@ -0,0 +1,83 @@ +openapi: 3.0.1 +info: + title: V1 + version: 2.0.0 +servers: + - url: "http://{server}:{port}/v1" + variables: + server: + default: localhost + port: + default: "8080" +paths: + /user: + post: + tags: + - user + operationId: postUser + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/User' + examples: + user01: + value: + id: "123" + name: Jessica + required: true + responses: + "201": + description: Created + content: + application/json: + schema: + $ref: '#/components/schemas/User' + examples: + Jessica: + value: + id: "123" + name: Jessica + "400": + description: BadRequest + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorPayload' +components: + schemas: + ErrorPayload: + required: + - message + - method + - path + - reason + - status + - timestamp + type: object + properties: + timestamp: + type: string + status: + type: integer + format: int64 + reason: + type: string + message: + type: string + path: + type: string + method: + type: string + User: + required: + - id + - name + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + additionalProperties: false diff --git a/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/service_file.bal b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/service_file.bal new file mode 100644 index 000000000..8d172aee0 --- /dev/null +++ b/openapi-integration-tests/src/test/resources/ballerina_sources/project_resource_info_with_service_contract_type/service_file.bal @@ -0,0 +1,58 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/http; +import ballerina/openapi; + +@http:ServiceConfig {basePath: "/v1"} +type OASServiceType service object { + *http:ServiceContract; + @openapi:ResourceInfo { + tags: ["user"], + examples: { + response: { + "201": { + "examples": { + "application/json": { + "Jessica": { + "value": { + "id" : "123", + "name": "Jessica" + } + } + } + } + } + }, + requestBody: { + "application/json": { + "user01": { + "value": { + "id": "123", + "name": "Jessica" + } + } + } + } + } + } + resource function post user(User user) returns User; +}; + +public type User record {| + int id; + string name; +|};