Skip to content

Commit

Permalink
chore: ignored error response in java and c#
Browse files Browse the repository at this point in the history
  • Loading branch information
sbansla committed Oct 29, 2024
1 parent e10a054 commit cde2d7d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/twilio/oai/StringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ private String convertFirstChar(final String inputWord, final UnaryOperator<Stri
? inputWord
: firstCharFunction.apply(inputWord.substring(0, 1)) + inputWord.substring(1);
}

public static boolean isSuccess(String responseCode) {
// Check if responseCode matches the success pattern for 2xx or 3xx
if (responseCode.matches("^[23](\\d{2}|x[0-9]|[0-9]x|xx)$")) {
return true;
}

// Check if the response code is an integer between 200 and 399
try {
int code = Integer.parseInt(responseCode);
return (code >= 200 && code <= 399);
} catch (NumberFormatException e) {
// Handle case where responseCode is not a valid integer
return false;
}
}
}
26 changes: 14 additions & 12 deletions src/main/java/com/twilio/oai/api/CsharpApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,20 @@ private List<CodegenParameter> getNonPathParams(List<CodegenParameter> allParams
public ApiResourceBuilder updateResponseModel(Resolver<CodegenProperty> codegenPropertyIResolver, Resolver<CodegenModel> codegenModelResolver) {
List<CodegenModel> responseModels = new ArrayList<>();
codegenOperationList.forEach(codegenOperation -> {
codegenOperation.responses.forEach(response -> {
String modelName = response.dataType;
if (response.dataType != null && response.dataType.startsWith(EnumConstants.CsharpDataTypes.LIST.getValue())){
modelName = response.baseType;
}
Optional<CodegenModel> responseModel = Utility.getModel(allModels, modelName, recordKey, codegenOperation);
if ((responseModel == null) || responseModel.isEmpty() || (Integer.parseInt(response.code) >= 400)) {
return;
}
codegenModelResolver.resolve(responseModel.get(), this);
responseModels.add(responseModel.get());
});
codegenOperation.responses.stream()
.filter(response -> StringHelper.isSuccess(response.code.trim()))
.forEach(response -> {
String modelName = response.dataType;
if (response.dataType != null && response.dataType.startsWith(EnumConstants.CsharpDataTypes.LIST.getValue())){
modelName = response.baseType;
}
Optional<CodegenModel> responseModel = Utility.getModel(allModels, modelName, recordKey, codegenOperation);
if ((responseModel == null) || responseModel.isEmpty() || (Integer.parseInt(response.code) >= 400)) {
return;
}
codegenModelResolver.resolve(responseModel.get(), this);
responseModels.add(responseModel.get());
});
});
this.apiResponseModels = getDistinctResponseModel(responseModels);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public ApiResourceBuilder updateResponseModel(Resolver<CodegenProperty> codegenP
jsonRequestBodyResolver.setResourceName(resourceName);
co.responses
.stream()
.filter(response -> SUCCESS.test(Integer.parseInt(response.code.trim())))
.filter(response -> StringHelper.isSuccess(response.code.trim()))
.map(response -> {
if (response.dataType != null && response.dataType.startsWith(EnumConstants.JavaDataTypes.LIST.getValue())) {
return response.baseType;
Expand Down

0 comments on commit cde2d7d

Please sign in to comment.