Skip to content

Commit

Permalink
[BP] Update SpringDoc json/yaml generator to make the results more de…
Browse files Browse the repository at this point in the history
…terministic. (#7574)

* Update SpringDoc json/yaml generator to make the results more deterministic.
This will make it easier to compare different versions between gn releases.

These are the properties that were enabled.

   springdoc.writer-with-order-by-keys=true
   springdoc.writer-with-default-pretty-printer=true

* Cleanup imports
  • Loading branch information
ianwallen committed Jan 3, 2024
1 parent 81fa709 commit 4281735
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions services/src/main/java/org/fao/geonet/api/OpenApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
package org.fao.geonet.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.PathUtils;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.api.AbstractOpenApiResource;
Expand All @@ -33,12 +31,9 @@
import org.springdoc.webmvc.core.RouterFunctionProvider;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -47,6 +42,7 @@
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

import static org.springdoc.core.Constants.*;
Expand All @@ -64,6 +60,8 @@
* springdoc.api-docs.enabled=true
* springdoc.api-docs.path=/api/doc
* springdoc.cache.disabled=true
* springdoc.writer-with-order-by-keys=true
* springdoc.writer-with-default-pretty-printer=true
*/
@RestController
public class OpenApiController extends AbstractOpenApiResource {
Expand Down Expand Up @@ -95,6 +93,11 @@ public OpenApiController(ObjectFactory<OpenAPIService> openAPIBuilderObjectFacto
"org.fao.geonet.api",
"org.fao.geonet.services.inspireatom",
"org.fao.geonet.monitor.service"}));

// Ensure open api document is consistently orders to make it easier to compare changes later.
springDocConfigProperties.setWriterWithOrderByKeys(true);
springDocConfigProperties.setWriterWithDefaultPrettyPrinter(true);

this.requestMappingHandlerMapping = requestMappingHandlerMapping;
this.servletContextProvider = servletContextProvider;
this.springSecurityOAuth2Provider = springSecurityOAuth2Provider;
Expand All @@ -103,20 +106,20 @@ public OpenApiController(ObjectFactory<OpenAPIService> openAPIBuilderObjectFacto

@Operation(hidden = true)
@GetMapping(value = "/{portal}/api/doc", produces = MediaType.APPLICATION_JSON_VALUE)
public String openapiJson(HttpServletRequest request, @Value(API_DOCS_URL) String apiDocsUrl)
public String openapiJson(HttpServletRequest request)
throws JsonProcessingException {
calculateServerUrl(request, apiDocsUrl);
setServerBaseUrl(request);
OpenAPI openAPI = this.getOpenApi();
return Json.mapper().writeValueAsString(openAPI);
return writeJsonValue(openAPI);
}

@Operation(hidden = true)
@GetMapping(value = "/{portal}/api/doc.yml", produces = APPLICATION_OPENAPI_YAML)
public String openapiYaml(HttpServletRequest request, @Value(DEFAULT_API_DOCS_URL_YAML) String apiDocsUrl)
public String openapiYaml(HttpServletRequest request)
throws JsonProcessingException {
calculateServerUrl(request, apiDocsUrl);
setServerBaseUrl(request);
OpenAPI openAPI = this.getOpenApi();
return Yaml.mapper().writeValueAsString(openAPI);
return writeYamlValue(openAPI);
}

@Override
Expand Down Expand Up @@ -177,9 +180,14 @@ protected boolean isRestController(Map<String, Object> restControllers,
|| !ModelAndView.class.isAssignableFrom(handlerMethod.getMethod().getReturnType()));
}

protected void calculateServerUrl(HttpServletRequest request, String apiDocsUrl) {
String requestUrl = decode(request.getRequestURL().toString());
String calculatedUrl = requestUrl.substring(0, requestUrl.length() - apiDocsUrl.length());
this.openAPIService.setServerBaseUrl(calculatedUrl);
private String getServerBaseUrl(HttpServletRequest request) {
String contextPath = request.getContextPath();
StringBuffer requestURL = request.getRequestURL();
String serverBaseUrl = requestURL.substring(0, requestURL.indexOf(contextPath) + contextPath.length());
return serverBaseUrl;
}

protected void setServerBaseUrl(HttpServletRequest request) {
this.openAPIService.setServerBaseUrl(getServerBaseUrl(request));
}
}

0 comments on commit 4281735

Please sign in to comment.