Skip to content

Commit

Permalink
Add support for enums in sevice generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshafedo committed May 10, 2023
1 parent 7872aeb commit bb0b50e
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 48 deletions.
28 changes: 14 additions & 14 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ puppycrawlCheckstyleVersion = 8.18
# Stdlib Level 01
stdlibConstraintVersion=1.1.0
stdlibIoVersion=1.4.0
stdlibRegexVersion=1.4.0
stdlibRegexVersion=1.4.2
stdlibTimeVersion=2.2.4
stdlibUrlVersion=2.2.3
stdlibXmldataVersion=2.4.1

# Stdlib Level 02
stdlibCryptoVersion=2.3.0
stdlibLogVersion=2.7.0
stdlibLogVersion=2.7.1-20230426-134900-2076e24
stdlibOsVersion=1.6.0
stdlibTaskVersion=2.3.1
stdlibTaskVersion=2.3.2

# Stdlib Level 03
stdlibCacheVersion=3.4.0
stdlibFileVersion=1.7.0
stdlibMimeVersion=2.7.0
stdlibUuidVersion=1.5.0
stdlibFileVersion=1.7.1-20230426-145900-748058d
stdlibMimeVersion=2.7.1-20230426-174700-fa17cd3
stdlibUuidVersion=1.5.1-20230426-134700-1b44f72

# Stdlib Level 04
stdlibAuthVersion=2.7.0
stdlibJwtVersion=2.7.0
stdlibOAuth2Version=2.7.0
stdlibAuthVersion=2.7.1-20230426-151300-6b9f314
stdlibJwtVersion=2.7.1-20230427-123000-f1f10bf
stdlibOAuth2Version=2.7.1-20230426-163900-b9c951b

# Stdlib Level 05
stdlibHttpVersion=2.7.0
stdlibHttpVersion=2.7.1-20230503-131100-9fadae2

# Stdlib Level 06
stdlibGrpcVersion=1.7.0
stdlibWebsocketVersion=2.7.0
stdlibWebsubVersion=2.7.0
stdlibGrpcVersion=1.7.1-20230428-115900-6f2f3a9
stdlibWebsocketVersion=2.7.1-20230428-133600-30df76b
stdlibWebsubVersion=2.7.1-20230428-125500-b33fd3e

# Stdlib Level 07
stdlibGraphqlVersion=1.7.0
stdlibGraphqlVersion=1.8.0-20230502-093000-c121a6a

# Ballerinax Observer
observeVersion=1.0.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,54 @@ public void withKeyWords() throws IOException, BallerinaOpenApiException {
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree("keywords.bal", syntaxTree);
}

@Test(description = "Tests for all the enum scenarios in resource function parameter generation:" +
"Use case 01 : Enum in query parameter" +
"Use case 02 : Enums in path parameter" +
"Use case 03 : Enum in header parameter" +
"Use case 04 : Enum in reusable parameter" +
"Use case 05 : Enum in parameter with referenced schema")
public void generateParametersWithEnums() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_enum.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.build();
BallerinaServiceGenerator ballerinaServiceGenerator = new BallerinaServiceGenerator(oasServiceMetadata);
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree("parameters_with_enum.bal", syntaxTree);
}

@Test(description = "Tests for all the nullable enum scenarios in resource function parameter generation:" +
"Use case 01 : Nullable enum in query parameter" +
"Use case 02 : Nullable enum in header parameter" +
"Use case 03 : Nullable enum in reusable parameter" +
"Use case 04 : Nullable enum in parameter with referenced schema")
public void generateParametersWithNullableEnums() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_nullable_enums.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.build();
BallerinaServiceGenerator ballerinaServiceGenerator = new BallerinaServiceGenerator(oasServiceMetadata);
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"parameters_with_nullable_enums.bal", syntaxTree);
}

@Test(description = "Test unsupported nullable path parameter with enums",
expectedExceptions = BallerinaOpenApiException.class,
expectedExceptionsMessageRegExp = "Path parameter value cannot be null.")
public void testNullablePathParamWithEnum() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/path_param_nullable.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.build();
BallerinaServiceGenerator ballerinaServiceGenerator = new BallerinaServiceGenerator(oasServiceMetadata);
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ service /v2 on ep0 {
# + return - returns can be any of following types
# Pet[] (successful operation)
# http:BadRequest (Invalid status value)
resource function get pet/findByStatus(string[] status) returns Pet[]|http:BadRequest {
resource function get pet/findByStatus(("available"|"pending"|"sold")[] status) returns Pet[]|http:BadRequest {
}
# Finds Pets by tags
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ballerina/http;

listener http:Listener ep0 = new (443, config = {host: "petstore3.swagger.io"});

service /api/v3 on ep0 {
# List meetings
#
# + group - Employee group
# + 'type - The meeting types. Scheduled, live or upcoming
# + status - Status values that need to be considered for filter
# + 'x\-date\-format - Date time format (cf. [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) & [leettime.de](http://leettime.de/))
# + 'x\-time\-zones - Time Zones of attendees
# + location - Meeting location
# + format - The response format you would like
# + return - returns can be any of following types
# MeetingList (HTTP Status Code:200. List of meetings returned.)
# http:NotFound (HTTP Status Code:404 User ID not found. Error Code:1001, User not exist or not belong to this account.)
resource function get users/meetings/["Admin"|"HR"|"Engineering" group](("available"|"pending")[]? status, @http:Header "UTC"|"LOCAL"|"OFFSET"|"EPOCH"|"LEET"? 'x\-date\-format, @http:Header ("IST"|"GMT"|"UTC")[] 'x\-time\-zones, "json"|"jsonp"|"msgpack"|"html"? format, "scheduled"|"live"|"upcoming" 'type = "live", RoomNo location = "R5") returns MeetingList|http:NotFound {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ballerina/http;

listener http:Listener ep0 = new (443, config = {host: "petstore3.swagger.io"});

service /api/v3 on ep0 {
# List meetings
#
# + 'type - The meeting types. Scheduled, live or upcoming
# + status - Status values that need to be considered for filter
# + 'x\-date\-format - Date time format (cf. [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) & [leettime.de](http://leettime.de/))
# + location - Meeting location
# + format - The response format you would like
# + return - returns can be any of following types
# MeetingList (HTTP Status Code:200. List of meetings returned.)
# http:NotFound (HTTP Status Code:404 User ID not found. Error Code:1001, User not exist or not belong to this account.)
resource function get users/meetings("scheduled"|"live"|"upcoming"? 'type, ("available"|"pending"?)[]? status, @http:Header "UTC"|"LOCAL"|"OFFSET"|"EPOCH"|"LEET"? 'x\-date\-format, "json"|"jsonp"|"msgpack"|"html"? format, RoomNo location = "R5") returns MeetingList|http:NotFound {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ service / on ep0 {
# + return - returns can be any of following types
# http:BadRequest (Invalid status value)
# http:Response (successful operation)
resource function get pet/findByStatus(string status = "available") returns http:BadRequest|http:Response {
resource function get pet/findByStatus("available"|"pending"|"sold" status = "available") returns http:BadRequest|http:Response {
}
# Description
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ paths:
- OFFSET
- EPOCH
- LEET
- description: Time Zones of attendees
in: header
required: true
name: X-Time-Zones
schema:
type: array
items:
enum:
- IST
- GMT
- UTC
- description: "Meeting location"
in: query
name: location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
openapi: 3.0.3
info:
title: Swagger Petstore - OpenAPI 3.0
description: |-
This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about
Swagger at [https://swagger.io](https://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
You can now help us improve the API whether it's by making changes to the definition itself or to the code.
That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
_If you're looking for the Swagger 2.0/OAS 2.0 version of Petstore, then click [here](https://editor.swagger.io/?url=https://petstore.swagger.io/v2/swagger.yaml). Alternatively, you can load via the `Edit > Load Petstore OAS 2.0` menu option!_
Some useful links:
- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
termsOfService: http://swagger.io/terms/
contact:
email: apiteam@swagger.io
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.11
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://petstore3.swagger.io/api/v3
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
- name: store
description: Access to Petstore orders
externalDocs:
description: Find out more about our store
url: http://swagger.io
- name: user
description: Operations about user
paths:
/users/meetings:
get:
description: >-
List all the meetings that were scheduled for a user (meeting
host). This API only supports scheduled meetings and thus, details on
instant meetings are not returned via this API.
**Scopes:** `meeting:read:admin` `meeting:read`
**[Rate Limit Label](https://marketplace.zoom.us/docs/api-reference/rate-limits#rate-limits):** `Medium`
summary: List meetings
operationId: listMeetings
parameters:
- description: "The meeting types. Scheduled, live or upcoming"
in: query
name: type
schema:
# default: live
enum:
- scheduled
- live
- upcoming
- null
type: string
nullable: true
- description: "Status values that need to be considered for filter"
in: query
name: status
schema:
type: array
items:
type: string
enum:
- "available"
- "pending"
- null
- description: Date time format (cf. [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) & [leettime.de](http://leettime.de/))
in: header
name: X-Date-Format
schema:
enum:
- UTC
- LOCAL
- OFFSET
- EPOCH
- LEET
- null
- description: "Meeting location"
in: query
name: location
schema:
$ref: '#/components/schemas/RoomNo'
- $ref: "#/components/parameters/responseFormat"
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/MeetingList'
description: "HTTP Status Code:200. List of meetings returned."
"404":
description: "HTTP Status Code:404 User ID not found. Error Code:1001, User not exist or not belong to this account."
tags:
- Meetings
components:
parameters:
responseFormat:
description: The response format you would like
in: query
name: format
schema:
enum:
- json
- jsonp
- msgpack
- html
- null
type: string
schemas:
RoomNo:
# How can we address when default value is there in a reusable enum
default: R5
enum:
- R1
- R3
- R5
- null
type: string
MeetingTypes:
type: string
default: live
enum:
- "scheduled"
- "live"
- "upcoming"
MeetingList:
description: List of meetings
title: Group List
type: object
properties:
meetings:
description: List of Meeting objects.
items:
$ref: '#/components/schemas/MeetingObject'
type: array
MeetingObject:
properties:
topic:
description: Meeting topic.
type: string
type:
description: "Meeting Type: 1 - Instant meeting. 2 - Scheduled meeting. 3 - Recurring meeting with no fixed time. 8 - Recurring meeting with fixed time."
type: integer
type: object
Loading

0 comments on commit bb0b50e

Please sign in to comment.