Skip to content

Commit

Permalink
Merge pull request #1713 from ballerina-platform/default-status-code-…
Browse files Browse the repository at this point in the history
…support

Add OpenAPI tool support for `http:DefaultStatusCodeResponse`
  • Loading branch information
lnash94 authored May 30, 2024
2 parents c05a37c + ca5219d commit 69bed2c
Show file tree
Hide file tree
Showing 128 changed files with 2,218 additions and 509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class Constants {
public static final String PORT = "port";
public static final String HTTP_REQUEST = "http:Request";
public static final String DEFAULT = "default";
public static final String DEFAULT_STATUS_CODE_RESPONSE = "DefaultStatusCodeResponse";
public static final String WILD_CARD_CONTENT_KEY = "*/*";
public static final String WILD_CARD_SUMMARY = "Any type of entity body";
public static final String MEDIA_TYPE = "mediaType";
Expand Down Expand Up @@ -184,8 +185,8 @@ public String toString() {
httpCodeMap.put("LoopDetected", "508");
httpCodeMap.put("NotExtended", "510");
httpCodeMap.put("NetworkAuthenticationRequired", "511");
httpCodeMap.put("NetworkAuthorizationRequired", "511"); //This status code was added since it is deprecated.
// TODO: remove this after fixing https://github.com/ballerina-platform/ballerina-standard-library/issues/4245
httpCodeMap.put("NetworkAuthorizationRequired", "511"); //This status code was added since it is deprecated.
HTTP_CODES = Collections.unmodifiableMap(httpCodeMap);
}
public static final Map<String, String> HTTP_STATUS_CODE_ERRORS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Map;
import java.util.Optional;

import static io.ballerina.openapi.service.mapper.Constants.DEFAULT;
import static io.ballerina.openapi.service.mapper.Constants.DEFAULT_STATUS_CODE_RESPONSE;
import static io.ballerina.openapi.service.mapper.Constants.HTTP;
import static io.ballerina.openapi.service.mapper.Constants.HTTP_CODES;
import static io.ballerina.openapi.service.mapper.Constants.STATUS_CODE_RESPONSE;
Expand Down Expand Up @@ -64,6 +66,9 @@ private static String getResponseCode(TypeSymbol typeSymbol, String defaultCode,
return entry.getValue();
}
}
if (isSubTypeOfBallerinaModuleType(DEFAULT_STATUS_CODE_RESPONSE, HTTP, typeSymbol, semanticModel)) {
return DEFAULT;
}
return defaultCode;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ stdlibJwtVersion=2.11.0
stdlibOAuth2Version=2.11.0

# Stdlib Level 05
stdlibHttpVersion=2.11.0
stdlibHttpVersion=2.11.1-20240527-111400-f6148d4

# Stdlib Level 06
stdlibGrpcVersion=1.11.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ service / on ep0 {
# Post operation for the path /user
#
# + return - Successful
resource function post user() returns OkJson {
resource function post user() returns JsonOk {
}
# Put operation for the path /user
#
Expand Down
2 changes: 1 addition & 1 deletion openapi-cli/src/test/resources/expected/noOperationId.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ service / on ep0 {
# Description
#
# + return - Successful
resource function post user() returns OkJson {
resource function post user() returns JsonOk {
}
}
2 changes: 1 addition & 1 deletion openapi-cli/src/test/resources/expected/nonEmptyPath.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ service / on ep0 {
# Post operation for the path /user
#
# + return - Successful
resource function post user() returns OkJson {
resource function post user() returns JsonOk {
}
# Put operation for the path /user
#
Expand Down
16 changes: 8 additions & 8 deletions openapi-cli/src/test/resources/expected/petstore.bal
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ service /v1 on ep0 {
#
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# Pets (An paged array of pets)
# http:Response (unexpected error)
resource function get pets(int? 'limit) returns Pets|http:Response {
# http:Ok (An paged array of pets)
# http:StatusCodeResponse (unexpected error)
resource function get pets(int? 'limit) returns Pets|ErrorDefault {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# Pets (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]() returns Dog|http:Response {
# http:Ok (Expected response to a valid request)
# http:StatusCodeResponse (unexpected error)
resource function get pets/[string petId]() returns Dog|ErrorDefault {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
resource function post pets() returns http:Created|http:Response {
# http:StatusCodeResponse (unexpected error)
resource function post pets() returns http:Created|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ service /api/v3 on ep0 {
#
# + payload - Create a new pet in the store
# + return - returns can be any of following types
# OkPetXml (Successful operation)
# PetXmlOk (Successful operation)
# http:MethodNotAllowed (Invalid input)
resource function post pet(@http:Payload Pet|xml|map<string> payload) returns OkPetXml|http:MethodNotAllowed {
resource function post pet(@http:Payload Pet|xml|map<string> payload) returns PetXmlOk|http:MethodNotAllowed {
}
# Update an existing pet
#
Expand Down
14 changes: 7 additions & 7 deletions openapi-cli/src/test/resources/expected_gen/generateSkeleton.bal
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ service /v1 on ep0 {
#
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# Pets (An paged array of pets)
# http:Response (unexpected error)
resource function get pets(int? 'limit) returns Pets|http:Response {
# http:Ok (An paged array of pets)
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(int? 'limit) returns Pets|ErrorDefault {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# http:Ok (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]() returns Dog|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets/[string petId]() returns Dog|ErrorDefault {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
resource function post pets() returns http:Created|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function post pets() returns http:Created|ErrorDefault {
}
}
6 changes: 3 additions & 3 deletions openapi-cli/src/test/resources/expected_gen/generated_bal.bal
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ service /v1 on ep0 {
# + petId - The id of the pet to retrieve
# + petName - The id of the pet to retrieve
# + return - returns can be any of following types
# Pets (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]/petName/[string petName]() returns Pets|http:Response {
# http:Ok (Expected response to a valid request)
# http:StatusCodeResponse (unexpected error)
resource function get pets/[string petId]/petName/[string petName]() returns Pets|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ service /v1 on ep0 {
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# http:Ok (An paged array of pets)
# http:Response (unexpected error)
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(http:Caller caller, http:Request request) returns error? {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# http:Ok (Expected response to a valid request)
# http:Response (unexpected error)
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets/[string petId](http:Caller caller, http:Request request) returns error? {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
# http:DefaultStatusCodeResponse (unexpected error)
resource function post pets(http:Caller caller, http:Request request) returns error? {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public type ClientHttp1Settings record {|
ProxyConfig proxy?;
|};

public type ErrorDefault record {|
*http:DefaultStatusCodeResponse;
Error body;
|};

public type Error record {
int code;
string message;
};

# Proxy server configurations to be used with the HTTP client endpoint.
public type ProxyConfig record {|
# Host name of the proxy server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// AUTO-GENERATED FILE.
// This file is auto-generated by the Ballerina OpenAPI tool.

import ballerina/http;

public type Pets Pet[];

public type Dog record{
public type ErrorDefault record {|
*http:DefaultStatusCodeResponse;
Error body;
|};

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public type ClientHttp1Settings record {|
ProxyConfig proxy?;
|};

public type ErrorDefault record {|
*http:DefaultStatusCodeResponse;
Error body;
|};

public type Error record {
int code;
string message;
};

# Proxy server configurations to be used with the HTTP client endpoint.
public type ProxyConfig record {|
# Host name of the proxy server
Expand Down
12 changes: 6 additions & 6 deletions openapi-cli/src/test/resources/expected_gen/licenses/service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ service /v1 on ep0 {
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# http:Ok (An paged array of pets)
# http:Response (unexpected error)
resource function get pets(int? 'limit) returns Pets|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(int? 'limit) returns Pets|ErrorDefault {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# http:Ok (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]() returns Dog|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets/[string petId]() returns Dog|ErrorDefault {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
resource function post pets() returns http:Created|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function post pets() returns http:Created|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ballerina/http;

type OASServiceType service object {
*http:Service;
resource function get pets(int? 'limit) returns Pets|http:Response;
resource function post pets() returns http:Created|http:Response;
resource function get pets/[string petId]() returns Dog|http:Response;
resource function get pets(int? 'limit) returns Pets|ErrorDefault;
resource function post pets() returns http:Created|ErrorDefault;
resource function get pets/[string petId]() returns Dog|ErrorDefault;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ballerina/http;

type OASServiceType service object {
*http:Service;
resource function get pets(int? 'limit) returns Pets|http:Response;
resource function post pets() returns http:Created|http:Response;
resource function get pets/[string petId]() returns Dog|http:Response;
resource function get pets(int? 'limit) returns Pets|ErrorDefault;
resource function post pets() returns http:Created|ErrorDefault;
resource function get pets/[string petId]() returns Dog|ErrorDefault;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ service OASServiceType /v1 on ep0 {
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# http:Ok (An paged array of pets)
# http:Response (unexpected error)
resource function get pets(int? 'limit) returns Pets|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(int? 'limit) returns Pets|ErrorDefault {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# http:Ok (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]() returns Dog|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets/[string petId]() returns Dog|ErrorDefault {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
resource function post pets() returns http:Created|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function post pets() returns http:Created|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ service OASServiceType /v1 on ep0 {
# + 'limit - How many items to return at one time (max 100)
# + return - returns can be any of following types
# http:Ok (An paged array of pets)
# http:Response (unexpected error)
resource function get pets(int? 'limit) returns Pets|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(int? 'limit) returns Pets|ErrorDefault {
}
# Info for a specific pet
#
# + petId - The id of the pet to retrieve
# + return - returns can be any of following types
# http:Ok (Expected response to a valid request)
# http:Response (unexpected error)
resource function get pets/[string petId]() returns Dog|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets/[string petId]() returns Dog|ErrorDefault {
}
# Create a pet
#
# + return - returns can be any of following types
# http:Created (Null response)
# http:Response (unexpected error)
resource function post pets() returns http:Created|http:Response {
# http:DefaultStatusCodeResponse (unexpected error)
resource function post pets() returns http:Created|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public type ClientHttp1Settings record {|
ProxyConfig proxy?;
|};

public type ErrorDefault record {|
*http:DefaultStatusCodeResponse;
Error body;
|};

public type Error record {
int code;
string message;
};

# Proxy server configurations to be used with the HTTP client endpoint.
public type ProxyConfig record {|
# Host name of the proxy server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ service /api on ep0 {
# + tags - tags to filter by
# + 'limit - maximum number of results to return
# + return - returns can be any of following types
# Pet[] (pet response)
# http:Response (unexpected error)
resource function get pets(string[]? tags, int? 'limit) returns Pet[]|http:Response {
# http:Ok (pet response)
# http:DefaultStatusCodeResponse (unexpected error)
resource function get pets(string[]? tags, int? 'limit) returns Pet[]|ErrorDefault {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ballerina/http;

type OASServiceType service object {
*http:Service;
resource function get pets(int? 'limit) returns Pets|http:Response;
resource function post pets() returns http:Created|http:Response;
resource function get pets/[string petId]() returns Dog|http:Response;
resource function get pets(int? 'limit) returns Pets|ErrorDefault;
resource function post pets() returns http:Created|ErrorDefault;
resource function get pets/[string petId]() returns Dog|ErrorDefault;
};
Loading

0 comments on commit 69bed2c

Please sign in to comment.