Skip to content

Commit

Permalink
Add missing @ApiResponse status for successful api search response. (g…
Browse files Browse the repository at this point in the history
…eonetwork#7594)

* Add missing @ApiResponse status for successful api search response.
Otherwise OpenAPI spec was missing response which caused issues when using codegen.

Fixed body parameters and removed httpEntity.getBody()
Also added example value for body.

* Remove @Schema from request body as maps object will be corrected in PR geonetwork#7611

* Revert request body back to string.
  • Loading branch information
ianwallen authored Jan 15, 2024
1 parent eaaa06d commit aed448c
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
import com.jayway.jsonpath.JsonPath;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jeeves.server.UserSession;
import jeeves.server.context.ServiceContext;
Expand Down Expand Up @@ -65,8 +69,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -273,9 +277,13 @@ private static boolean hasOperation(ObjectNode doc, ReservedGroup group, Reserve
summary = "Search endpoint",
description = "See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html for search parameters details.")
@RequestMapping(value = "/search/records/_search",
method = {
RequestMethod.POST
})
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Search results.",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(type = "string")))
})
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public void search(
Expand All @@ -292,22 +300,28 @@ public void search(
HttpServletRequest request,
@Parameter(hidden = true)
HttpServletResponse response,
@RequestBody(description = "JSON request based on Elasticsearch API.")
String body,
@Parameter(hidden = true)
HttpEntity<String> httpEntity) throws Exception {
@RequestBody
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "JSON request based on Elasticsearch API.",
content = @Content(examples = {
@ExampleObject(value = "{\"query\":{\"match\":{\"_id\":\"catalogue_uuid\"}}}")
}))
String body) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
call(context, httpSession, request, response, SEARCH_ENDPOINT, httpEntity.getBody(), bucket, relatedTypes);
call(context, httpSession, request, response, SEARCH_ENDPOINT, body, bucket, relatedTypes);
}


@io.swagger.v3.oas.annotations.Operation(
summary = "Search endpoint",
description = "See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html for search parameters details.")
@RequestMapping(value = "/search/records/_msearch",
method = {
RequestMethod.POST
})
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Search results.",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(type = "string")))
})
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public void msearch(
Expand All @@ -324,12 +338,14 @@ public void msearch(
HttpServletRequest request,
@Parameter(hidden = true)
HttpServletResponse response,
@RequestBody(description = "JSON request based on Elasticsearch API.")
String body,
@Parameter(hidden = true)
HttpEntity<String> httpEntity) throws Exception {
@RequestBody
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "JSON request based on Elasticsearch API.",
content = @Content(examples = {
@ExampleObject(value = "{\"query\":{\"match\":{\"_id\":\"catalogue_uuid\"}}}")
}))
String body) throws Exception {
ServiceContext context = ApiUtils.createServiceContext(request);
call(context, httpSession, request, response, MULTISEARCH_ENDPOINT, httpEntity.getBody(), bucket, relatedTypes);
call(context, httpSession, request, response, MULTISEARCH_ENDPOINT, body, bucket, relatedTypes);
}


Expand All @@ -342,7 +358,13 @@ public void msearch(
@RequestMapping(value = "/search/records/{endPoint}",
method = {
RequestMethod.POST, RequestMethod.GET
})
},
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Search results.",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(type = "string")))
})
@ResponseStatus(value = HttpStatus.OK)
@PreAuthorize("hasAuthority('Administrator')")
@ResponseBody
Expand All @@ -357,16 +379,18 @@ public void call(
HttpServletRequest request,
@Parameter(hidden = true)
HttpServletResponse response,
@RequestBody(description = "JSON request based on Elasticsearch API.")
String body,
@Parameter(hidden = true)
HttpEntity<String> httpEntity) throws Exception {
@RequestBody
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "JSON request based on Elasticsearch API.",
content = @Content(examples = {
@ExampleObject(value = "{\"query\":{\"match\":{\"_id\":\"catalogue_uuid\"}}}")
}))
String body) throws Exception {

ServiceContext context = ApiUtils.createServiceContext(request);
call(context, httpSession, request, response, endPoint, httpEntity.getBody(), bucket, null);
call(context, httpSession, request, response, endPoint, body, bucket, null);
}

public void call(ServiceContext context, HttpSession httpSession, HttpServletRequest request,
private void call(ServiceContext context, HttpSession httpSession, HttpServletRequest request,
HttpServletResponse response,
String endPoint, String body,
String selectionBucket,
Expand Down

0 comments on commit aed448c

Please sign in to comment.