Skip to content

Commit

Permalink
Add a sample request body for default API resource
Browse files Browse the repository at this point in the history
  • Loading branch information
rosensilva committed Oct 5, 2023
1 parent 0198213 commit 131e12c
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,42 @@ private void addDefaultResponseAndPathItem(PathItem pathItem, Operation operatio
}
}

/**
* Add the default request body
*
* @param operation Operation object that contains params (path, query and body)
* @param methodEntry Context fof the resource.
*/
private void addDefaultRequestBody(Operation operation, Map.Entry methodEntry) {
if (operation.getRequestBody() == null) {
RequestBody requestBody = new RequestBody();
requestBody.description("Sample Payload");
requestBody.setRequired(false);

MediaType mediaType = new MediaType();
Schema bodySchema = new Schema();
bodySchema.setType("object");

Map<String, Schema> inputProperties = new HashMap<>();
ObjectSchema objectSchema = new ObjectSchema();

bodySchema.setProperties(inputProperties);
inputProperties.put("payload", objectSchema);
mediaType.setSchema(bodySchema);
Content content = new Content();
content.addMediaType("application/json", mediaType);
requestBody.setContent(content);

switch ((String) methodEntry.getKey()) {
case OPERATION_HTTP_POST:
case OPERATION_HTTP_PUT:
case OPERATION_HTTP_PATCH:
operation.setRequestBody(requestBody);
break;
}
}
}

/**
* Update a given swagger definition of the Synapse API.
*
Expand Down Expand Up @@ -622,6 +658,7 @@ private void populateParameters(PathItem pathItem, Map<String, Object> methodMap
} else {
// no parameters defined ( default resource in the API )
addDefaultResponseAndPathItem(pathItem, operation, methodEntry);
addDefaultRequestBody(operation, methodEntry);
}
}
}
Expand Down

0 comments on commit 131e12c

Please sign in to comment.