Skip to content

Commit

Permalink
fix(document-handling): refactor the Objectmapper supplier and make i…
Browse files Browse the repository at this point in the history
…t private so only copy can be created
  • Loading branch information
mathias-vandaele committed Dec 13, 2024
1 parent dae09d7 commit 7da40a1
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ public class OutboundConnectorsAutoConfiguration {

private static final Logger LOG =
LoggerFactory.getLogger(OutboundConnectorsAutoConfiguration.class);

@Value("${camunda.connector.secretprovider.discovery.enabled:true}")
Boolean secretProviderLookupEnabled;

@Value("${camunda.connector.secretprovider.environment.prefix:}")
String environmentSecretProviderPrefix;

@Value(
"${camunda.connector.secretprovider.console.endpoint:https://cluster-api.cloud.camunda.io/secrets}")
String consoleSecretsApiEndpoint;

@Value("${camunda.connector.secretprovider.console.audience:secrets.camunda.io}")
String consoleSecretsApiAudience;

Expand Down Expand Up @@ -159,8 +163,7 @@ public ConsoleSecretApiClient consoleSecretApiClient(CamundaClientProperties cli
@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper(DocumentFactory documentFactory) {
ConnectorsObjectMapperSupplier.getCopy(
documentFactory, DocumentModuleSettings.create());
ConnectorsObjectMapperSupplier.getCopy(documentFactory, DocumentModuleSettings.create());
return ConnectorsObjectMapperSupplier.getCopy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocument.DocumentModuleSettings;
import io.camunda.connector.feel.jackson.JacksonModuleFeelFunction;
import io.camunda.document.factory.DocumentFactory;
import io.camunda.document.factory.DocumentFactoryImpl;
import io.camunda.document.store.InMemoryDocumentStore;

/** Default ObjectMapper supplier to be used by the connector runtime. */
public class ConnectorsObjectMapperSupplier {
Expand All @@ -46,8 +44,7 @@ public class ConnectorsObjectMapperSupplier {
private ConnectorsObjectMapperSupplier() {}

public static ObjectMapper getCopy() {
return DEFAULT_MAPPER
.copy();
return DEFAULT_MAPPER.copy();
}

public static ObjectMapper getCopy(DocumentFactory factory, DocumentModuleSettings settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public void uriAuthenticationReceiveMessageTest() throws Exception {

private void assertIntermediateCatchEventUsingModel(BpmnModelInstance model) throws Exception {
Object expectedJsonResponse =
ConnectorsObjectMapperSupplier.getCopy().readValue(
"{\"message\":{\"consumerTag\":\"myConsumerTag\",\"body\":{\"foo\": {\"bar\": \"barValue\"}},\"properties\":{}}}",
Object.class);
ConnectorsObjectMapperSupplier.getCopy()
.readValue(
"{\"message\":{\"consumerTag\":\"myConsumerTag\",\"body\":{\"foo\": {\"bar\": \"barValue\"}},\"properties\":{}}}",
Object.class);

processStateStore.update(mockProcessDefinition(model));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ void execute_shouldSetConnectTime(final String input) throws Exception {
objectMapper.readValue(input, ObjectNode.class).get("graphql").toString(),
GraphQLRequest.GraphQL.class)
.connectionTimeoutInSeconds();
var graphQLRequestMapper =
new GraphQLRequestMapper(ConnectorsObjectMapperSupplier.getCopy());
var graphQLRequestMapper = new GraphQLRequestMapper(ConnectorsObjectMapperSupplier.getCopy());

// when
var request =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public HttpCommonRequest toCloudFunctionRequest(final HttpCommonRequest request)
try {
// Using the JsonHttpContent cannot work with an element on the root content,
// hence write it ourselves:
String contentAsJson =
ConnectorsObjectMapperSupplier.getCopy().writeValueAsString(request);
String contentAsJson = ConnectorsObjectMapperSupplier.getCopy().writeValueAsString(request);
String token = credentials.getOAuthToken(getProxyFunctionUrl());
return createCloudFunctionRequest(contentAsJson, token);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ public void shouldSetJsonBody_whenBodySupportedAndContentTypeNotProvided() throw
assertThat(httpRequest.getEntity().getContentType())
.isEqualTo(ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8).toString());
var jsonNode =
ConnectorsObjectMapperSupplier.getCopy().readTree(
httpRequest.getEntity().getContent());
ConnectorsObjectMapperSupplier.getCopy().readTree(httpRequest.getEntity().getContent());
assertThat(jsonNode.get("key").asText()).isEqualTo("value");
}

Expand All @@ -374,8 +373,7 @@ public void shouldNotSetJsonBody_whenBodySupportedAndContentTypeProvided() throw
assertThat(httpRequest.getEntity().getContentType())
.isEqualTo(ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8).toString());
var jsonNode =
ConnectorsObjectMapperSupplier.getCopy().readTree(
httpRequest.getEntity().getContent());
ConnectorsObjectMapperSupplier.getCopy().readTree(httpRequest.getEntity().getContent());
assertThat(jsonNode.get("key").asText()).isEqualTo("value");
}

Expand All @@ -400,8 +398,7 @@ public void shouldSetJsonBody_whenBodySupportedAndContentTypeProvided() throws E
assertThat(httpRequest.getEntity().getContentType())
.isEqualTo(ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8).toString());
var jsonNode =
ConnectorsObjectMapperSupplier.getCopy().readTree(
httpRequest.getEntity().getContent());
ConnectorsObjectMapperSupplier.getCopy().readTree(httpRequest.getEntity().getContent());
assertThat(jsonNode.get("key").asText()).isEqualTo("value");
}

Expand All @@ -427,8 +424,7 @@ public void shouldSetJsonBody_whenBodySupportedAndContentTypeProvidedAndBodyIsMa
assertThat(httpRequest.getEntity().getContentType())
.isEqualTo(ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8).toString());
var jsonNode =
ConnectorsObjectMapperSupplier.getCopy().readTree(
httpRequest.getEntity().getContent());
ConnectorsObjectMapperSupplier.getCopy().readTree(httpRequest.getEntity().getContent());
assertThat(jsonNode.get("key").asText()).isEqualTo("value");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public void shouldHandleError_whenCloudFunctionEnabled() throws Exception {
response.setHeaders(headers);
response.setEntity(
new StringEntity(
ConnectorsObjectMapperSupplier.getCopy().writeValueAsString(
new ErrorResponse("500", "Custom message", null))));
ConnectorsObjectMapperSupplier.getCopy()
.writeValueAsString(new ErrorResponse("500", "Custom message", null))));

// when
HttpCommonResult result = handler.handleResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ public Response transform(Response response, ServeEvent serveEvent) {
.but()
.status(200)
.body(
ConnectorsObjectMapperSupplier.getCopy().writeValueAsString(
httpService.executeConnectorRequest(request)))
ConnectorsObjectMapperSupplier.getCopy()
.writeValueAsString(httpService.executeConnectorRequest(request)))
.build();
} catch (ConnectorException e) {
try {
return new Response.Builder()
.status(500)
.headers(new HttpHeaders(new HttpHeader("Content-Type", "application/json")))
.body(
ConnectorsObjectMapperSupplier.getCopy().writeValueAsString(
new ErrorResponse(e.getErrorCode(), e.getMessage(), e.getErrorVariables())))
ConnectorsObjectMapperSupplier.getCopy()
.writeValueAsString(
new ErrorResponse(e.getErrorCode(), e.getMessage(), e.getErrorVariables())))
.build();
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public void shouldConvertToCloudFunctionRequest() throws IOException {
assertThat(cloudFunctionRequest.getHeaders().orElse(Map.of()))
.containsEntry("Content-Type", "application/json");
Map<String, Object> body =
ConnectorsObjectMapperSupplier.getCopy().readValue(
(String) cloudFunctionRequest.getBody(), Map.class);
ConnectorsObjectMapperSupplier.getCopy()
.readValue((String) cloudFunctionRequest.getBody(), Map.class);
assertThat(body).containsEntry("url", "theUrl");
assertThat(body).containsEntry("method", "POST");
assertThat(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class GcpSecretManagerSecretProvider implements SecretProvider {

/** Secrets used as fallback if SecretProvider is loaded via SPI */
public static final String SECRETS_PROJECT_ENV_NAME = "SECRETS_PROJECT_ID";

public static final String SECRETS_PREFIX_ENV_NAME = "SECRETS_PREFIX";
public static final String CLUSTER_ID_ENV_NAME = "CAMUNDA_CLUSTER_ID";
public static final String SECRETS_CACHE_MILLIS_ENV_NAME =
Expand Down

0 comments on commit 7da40a1

Please sign in to comment.