Skip to content

Commit

Permalink
Fix http API argument issues
Browse files Browse the repository at this point in the history
`
  • Loading branch information
warunalakshitha committed Oct 22, 2024
1 parent 866a56a commit 7f75123
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public static Object[] getSignatureParameters(Resource resource, HttpCarbonMessa
ParamHandler paramHandler = resource.getParamHandler();
Type[] parameterTypes = getParameterTypes(resource.getBalResource());
int sigParamCount = parameterTypes.length;
Object[] paramFeed = new Object[sigParamCount * 2];
Object[] paramFeed = new Object[sigParamCount];
boolean treatNilableAsOptional = resource.isTreatNilableAsOptional();
// Following was written assuming that they are validated
for (Parameter param : paramHandler.getParamList()) {
Expand All @@ -343,37 +343,32 @@ public static Object[] getSignatureParameters(Resource resource, HttpCarbonMessa
case HttpConstants.CALLER:
int index = ((NonRecurringParam) param).getIndex();
httpCaller.set(HttpConstants.CALLER_PRESENT_FIELD, true);
paramFeed[index++] = httpCaller;
paramFeed[index] = true;
paramFeed[index] = httpCaller;
break;
case HttpConstants.REQUEST_CONTEXT:
index = ((NonRecurringParam) param).getIndex();
paramFeed[index++] = requestCtx;
paramFeed[index] = true;
paramFeed[index] = requestCtx;
break;
case HttpConstants.STRUCT_GENERIC_ERROR:
if (error == null) {
error = createError();
}
index = ((NonRecurringParam) param).getIndex();
paramFeed[index++] = error;
paramFeed[index] = true;
paramFeed[index] = error;
break;
case HttpConstants.REQUEST:
if (inRequest == null) {
inRequest = createRequest(httpCarbonMessage, entityObj);
}
index = ((NonRecurringParam) param).getIndex();
paramFeed[index++] = inRequest;
paramFeed[index] = true;
paramFeed[index] = inRequest;
break;
case HttpConstants.HEADERS:
if (inRequest == null) {
inRequest = createRequest(httpCarbonMessage, entityObj);
}
index = ((NonRecurringParam) param).getIndex();
paramFeed[index++] = createHeadersObject(inRequest);
paramFeed[index] = true;
paramFeed[index] = createHeadersObject(inRequest);
break;
case HttpConstants.QUERY_PARAM:
((AllQueryParams) param).populateFeed(httpCarbonMessage, paramHandler, paramFeed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public void populateFeed(Object[] paramFeed, HttpCarbonMessage httpCarbonMessage
}
}

paramFeed[index++] = pathParam.validateConstraints(castedPathValue);
paramFeed[index] = true;
paramFeed[index] = pathParam.validateConstraints(castedPathValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public String getTypeName() {
}

public int getIndex() {
return this.index * 2;
return this.index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,57 +136,53 @@ public void populateFeed(BObject inRequest, HttpCarbonMessage httpCarbonMessage,
// Check if datasource is already available from interceptor service read
// TODO : Validate the dataSource type with payload type and populate
if (dataSource != null) {
index = populateFeedWithAlreadyBuiltPayload(paramFeed, inRequestEntity, index, payloadType, dataSource);
populateFeedWithAlreadyBuiltPayload(paramFeed, inRequestEntity, index, payloadType, dataSource);
} else {
index = populateFeedWithFreshPayload(httpCarbonMessage, paramFeed, inRequestEntity, index, payloadType);
populateFeedWithFreshPayload(httpCarbonMessage, paramFeed, inRequestEntity, index, payloadType);
}
paramFeed[index] = true;
}

private int populateFeedWithAlreadyBuiltPayload(Object[] paramFeed, BObject inRequestEntity, int index,
private void populateFeedWithAlreadyBuiltPayload(Object[] paramFeed, BObject inRequestEntity, int index,
Type payloadType, Object dataSource) {
try {
switch (payloadType.getTag()) {
case ARRAY_TAG:
int actualTypeTag = TypeUtils.getReferredType(((ArrayType) payloadType).getElementType()).getTag();
if (actualTypeTag == TypeTags.BYTE_TAG) {
paramFeed[index++] = validateConstraints(dataSource);
paramFeed[index] = validateConstraints(dataSource);
} else if (actualTypeTag == TypeTags.RECORD_TYPE_TAG) {
dataSource = JsonToRecordConverter.convert(payloadType, inRequestEntity, readonly);
paramFeed[index++] = validateConstraints(dataSource);
paramFeed[index] = validateConstraints(dataSource);
} else {
throw HttpUtil.createHttpError("incompatible element type found inside an array " +
((ArrayType) payloadType).getElementType().getName());
}
break;
case TypeTags.RECORD_TYPE_TAG:
dataSource = JsonToRecordConverter.convert(payloadType, inRequestEntity, readonly);
paramFeed[index++] = validateConstraints(dataSource);
paramFeed[index] = validateConstraints(dataSource);
break;
default:
paramFeed[index++] = validateConstraints(dataSource);
paramFeed[index] = validateConstraints(dataSource);
}
} catch (BError ex) {
String message = "data binding failed: " + HttpUtil.getPrintableErrorMsg(ex);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_PAYLOAD_BINDING_LISTENER_ERROR, message);
}
return index;
}

private int populateFeedWithFreshPayload(HttpCarbonMessage inboundMessage, Object[] paramFeed,
private void populateFeedWithFreshPayload(HttpCarbonMessage inboundMessage, Object[] paramFeed,
BObject inRequestEntity, int index, Type payloadType) {
try {
String contentType = HttpUtil.getContentTypeFromTransportMessage(inboundMessage);
AbstractPayloadBuilder payloadBuilder = getBuilder(contentType, payloadType);
Object payloadBuilderValue = payloadBuilder.getValue(inRequestEntity, this.readonly);
paramFeed[index] = validateConstraints(payloadBuilderValue);
inboundMessage.setProperty(HttpConstants.ENTITY_OBJ, inRequestEntity);
return ++index;
} catch (BError ex) {
String typeName = ex.getType().getName();
if (MimeConstants.NO_CONTENT_ERROR.equals(typeName) && this.nilable) {
paramFeed[index] = null;
return ++index;
}
if (INTERNAL_PAYLOAD_VALIDATION_LISTENER_ERROR.getErrorName().equals(typeName)) {
throw ex;
Expand Down

0 comments on commit 7f75123

Please sign in to comment.