Skip to content

Commit

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

Add OAS mapping for HTTP status code errors
  • Loading branch information
TharmiganK authored Feb 14, 2024
2 parents 2245a6a + 5ec92e5 commit a8f3e4d
Show file tree
Hide file tree
Showing 28 changed files with 1,345 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,51 @@ public String toString() {
// TODO: remove this after fixing https://github.com/ballerina-platform/ballerina-standard-library/issues/4245
HTTP_CODES = Collections.unmodifiableMap(httpCodeMap);
}
public static final Map<String, String> HTTP_STATUS_CODE_ERRORS;
static {
Map<String, String> httpErrorCodeMap = new HashMap<>();
httpErrorCodeMap.put("DefaultStatusCodeError", "default");
httpErrorCodeMap.put("BadRequestError", "400");
httpErrorCodeMap.put("UnauthorizedError", "401");
httpErrorCodeMap.put("PaymentRequiredError", "402");
httpErrorCodeMap.put("ForbiddenError", "403");
httpErrorCodeMap.put("NotFoundError", "404");
httpErrorCodeMap.put("MethodNotAllowedError", "405");
httpErrorCodeMap.put("NotAcceptableError", "406");
httpErrorCodeMap.put("ProxyAuthenticationRequiredError", "407");
httpErrorCodeMap.put("RequestTimeOutError", "408");
httpErrorCodeMap.put("ConflictError", "409");
httpErrorCodeMap.put("GoneError", "410");
httpErrorCodeMap.put("LengthRequiredError", "411");
httpErrorCodeMap.put("PreconditionFailedError", "412");
httpErrorCodeMap.put("PayloadTooLargeError", "413");
httpErrorCodeMap.put("URITooLongError", "414");
httpErrorCodeMap.put("UnsupportedMediaTypeError", "415");
httpErrorCodeMap.put("RangeNotSatisfiableError", "416");
httpErrorCodeMap.put("ExpectationFailedError", "417");
httpErrorCodeMap.put("MisdirectedRequestError", "421");
httpErrorCodeMap.put("UnprocessableEntityError", "422");
httpErrorCodeMap.put("LockedError", "423");
httpErrorCodeMap.put("FailedDependencyError", "424");
httpErrorCodeMap.put("TooEarlyError", "425");
httpErrorCodeMap.put("UpgradeRequiredError", "426");
httpErrorCodeMap.put("PreconditionRequiredError", "428");
httpErrorCodeMap.put("TooManyRequestsError", "429");
httpErrorCodeMap.put("RequestHeaderFieldsTooLargeError", "431");
httpErrorCodeMap.put("UnavailableDueToLegalReasonsError", "451");
httpErrorCodeMap.put("InternalServerErrorError", "500");
httpErrorCodeMap.put("NotImplementedError", "501");
httpErrorCodeMap.put("BadGatewayError", "502");
httpErrorCodeMap.put("ServiceUnavailableError", "503");
httpErrorCodeMap.put("GatewayTimeoutError", "504");
httpErrorCodeMap.put("HTTPVersionNotSupportedError", "505");
httpErrorCodeMap.put("VariantAlsoNegotiatesError", "506");
httpErrorCodeMap.put("InsufficientStorageError", "507");
httpErrorCodeMap.put("LoopDetectedError", "508");
httpErrorCodeMap.put("NotExtendedError", "510");
httpErrorCodeMap.put("NetworkAuthenticationRequiredError", "511");
HTTP_STATUS_CODE_ERRORS = Collections.unmodifiableMap(httpErrorCodeMap);
}
public static final Map<String, String> HTTP_CODE_DESCRIPTIONS;
static {
Map<String, String> httpCodeDescriptionMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ public void setConstraints() {
}
Map<String, Schema> schemas = components.getSchemas();
for (Map.Entry<String, Schema> schemaEntry : schemas.entrySet()) {
TypeDefinitionNode typeDefinitionNode = moduleMemberVisitor.getTypeDefinitionNode(schemaEntry.getKey());
if (Objects.isNull(typeDefinitionNode)) {
Optional<TypeDefinitionNode> typeDefNodeOpt = moduleMemberVisitor.getTypeDefinitionNode(
schemaEntry.getKey());
if (typeDefNodeOpt.isEmpty()) {
continue;
}
TypeDefinitionNode typeDefinitionNode = typeDefNodeOpt.get();
if (typeDefinitionNode.metadata().isPresent()) {
ConstraintAnnotation.ConstraintAnnotationBuilder constraintBuilder =
new ConstraintAnnotation.ConstraintAnnotationBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ballerina.openapi.service.mapper.utils.MapperCommonUtils;

import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -49,12 +50,12 @@ public Set<ListenerDeclarationNode> getListenerDeclarationNodes() {
return listenerDeclarationNodes;
}

public TypeDefinitionNode getTypeDefinitionNode(String typeName) {
public Optional<TypeDefinitionNode> getTypeDefinitionNode(String typeName) {
for (TypeDefinitionNode typeDefinitionNode : typeDefinitionNodes) {
if (MapperCommonUtils.unescapeIdentifier(typeDefinitionNode.typeName().text()).equals(typeName)) {
return typeDefinitionNode;
return Optional.of(typeDefinitionNode);
}
}
return null;
return Optional.empty();
}
}
Loading

0 comments on commit a8f3e4d

Please sign in to comment.