Skip to content

Commit

Permalink
Shim ObjectMapper and add diagnostic version info to exceptions (#23441)
Browse files Browse the repository at this point in the history
* Shim for ObjectMapper and use shim within azure-core

Co-authored-by: Srikanta <srnagar@microsoft.com>
  • Loading branch information
lmolkova and srnagar authored Sep 14, 2021
1 parent 239a340 commit bf82b20
Show file tree
Hide file tree
Showing 64 changed files with 1,481 additions and 502 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<suppress checks="Header" files="com.azure.core.http.netty.implementation.HttpProxyHandler"/>

<!-- Code was copied from FasterXML.com and use their license instead of our usual one. -->
<suppress checks="Header" files="com.azure.core.implementation.OptionModule"/>
<suppress checks="Header" files="com.azure.core.implementation.OptionPropertiesModifier"/>
<suppress checks="Header" files="com.azure.core.implementation.OptionBeanPropertyWriter"/>
<suppress checks="Header" files="com.azure.core.implementation.OptionTypeModifier"/>
<suppress checks="Header" files="com.azure.core.implementation.OptionSerializerProvider"/>
<suppress checks="Header" files="com.azure.core.implementation.OptionSerializer"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionModule"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionPropertiesModifier"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionBeanPropertyWriter"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionTypeModifier"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionSerializerProvider"/>
<suppress checks="Header" files="com.azure.core.implementation.jackson.OptionSerializer"/>

<!-- empty Enum Singleton Pattern -->
<suppress checks="NoWhitespaceBefore" files="Messages.java"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,10 @@
<!-- Serializers define non-transient non-serializable instance field -->
<Match>
<Or>
<Class name="com.azure.core.util.serializer.FlatteningSerializer"/>
<Class name="com.azure.core.util.serializer.FlatteningDeserializer"/>
<Class name="com.azure.core.util.serializer.AdditionalPropertiesSerializer"/>
<Class name="com.azure.core.util.serializer.AdditionalPropertiesDeserializer"/>
<Class name="com.azure.core.implementation.jackson.FlatteningSerializer"/>
<Class name="com.azure.core.implementation.jackson.FlatteningDeserializer"/>
<Class name="com.azure.core.implementation.jackson.AdditionalPropertiesSerializer"/>
<Class name="com.azure.core.implementation.jackson.AdditionalPropertiesDeserializer"/>
</Or>
<Bug pattern="SE_BAD_FIELD"/>
</Match>
Expand Down
8 changes: 5 additions & 3 deletions sdk/core/azure-core-jackson-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Azure Core Jackson Tests is a test package that tests `azure-core` library again

## Getting started

This package is intended to run in Live Test Azure Pipeline (`java - core - test`) under `jackson_versions` test name), but you can run it locally by setting `AZURE_CORE_TEST_JACKSON_VERSION` environment variable.
This package is intended to run in Live Test Azure Pipeline (`java - core - test`) under `jackson_supported_versions` and
`jackson_unsupported_versions` test name), but you can run it locally by setting `AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION`
or `AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION` environment variables.

## Key concepts

Expand All @@ -15,14 +17,14 @@ Here's how you can test arbitrary Jackson version from `azure-core-jackson-tests
Windows:

```powershell
PS> $env:AZURE_CORE_TEST_JACKSON_VERSION="2.12.1"
PS> $env:AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION="2.12.1"
PS> mvn test
```

Linux:

```bash
$ export AZURE_CORE_TEST_JACKSON_VERSION="2.12.2"
$ export AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION="2.12.2"
$ mvn test
```

Expand Down
120 changes: 96 additions & 24 deletions sdk/core/azure-core-jackson-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.21.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core;current} -->
<scope>test</scope>
</dependency>

<dependency>
Expand Down Expand Up @@ -159,40 +160,34 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<dependenciesToScan>
<dependency>com.azure:azure-core</dependency>
</dependenciesToScan>
<excludes>
<!-- TODO(limolkova) requires azure-core test resources-->
<exclude>**/RestProxyXMLTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>java-lts</id>
<id>default</id>
<activation>
<jdk>[11,)</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

</profile>

<profile>
<id>jackson-version-test-matrix</id>
<id>jackson-supported-version-test-matrix</id>
<activation>
<property><name>env.AZURE_CORE_TEST_JACKSON_VERSION</name></property>
<property><name>env.AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION</name></property>
</activation>
<properties>
<jackson.version>${env.AZURE_CORE_TEST_JACKSON_VERSION}</jackson.version>
<jackson.version>${env.AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION}</jackson.version>
</properties>
<dependencies>
<!--Have to redeclare dependencies here to allow x-version-update script to work properly-->
Expand Down Expand Up @@ -226,8 +221,85 @@
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<dependenciesToScan>
<dependency>com.azure:azure-core</dependency>
</dependenciesToScan>
<excludes>
<!-- TODO(limolkova) requires azure-core test resources-->
<exclude>**/RestProxyXMLTests.java</exclude>
<exclude>**/UnsupportedJacksonVersionsTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>jackson-unsupported-version-test</id>
<activation>
<property><name>env.AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION</name></property>
</activation>
<properties>
<jackson.version>${env.AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION}</jackson.version>
</properties>
<dependencies>
<!--Have to redeclare dependencies here to allow x-version-update script to work properly-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<includes>
<include>**/UnsupportedJacksonVersionsTests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

</profile>
</profiles>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.implementation.jackson;

import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class UnsupportedJacksonVersionsTests {
@Test
public void testUnsupportedVersion() {
String jacksonVersionString = ObjectMapper.class.getPackage().getImplementationVersion();
String azureCoreVersion = CoreUtils
.getProperties("azure-core.properties")
.getOrDefault("version", null);

JacksonVersion version = JacksonVersion.getInstance();
String helpInfo = version.getHelpInfo();
assertTrue(helpInfo.contains("jackson-annotations=" + jacksonVersionString));
assertTrue(helpInfo.contains("jackson-core=" + jacksonVersionString));
assertTrue(helpInfo.contains("jackson-databind=" + jacksonVersionString));
assertTrue(helpInfo.contains("jackson-dataformat-xml=" + jacksonVersionString));
assertTrue(helpInfo.contains("jackson-datatype-jsr310=" + jacksonVersionString));
assertTrue(helpInfo.contains("azure-core=" + azureCoreVersion));

Error error = assertThrows(Error.class, () -> new JacksonAdapter());
assertTrue(error.getMessage().contains(helpInfo));
}
}
1 change: 1 addition & 0 deletions sdk/core/azure-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
--add-opens com.azure.core/com.azure.core.implementation.models.jsonflatten=com.fasterxml.jackson.databind
--add-opens com.azure.core/com.azure.core.implementation.models.jsonflatten=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.implementation.serializer=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.implementation.jackson=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.models=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.util=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.util.logging=ALL-UNNAMED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.implementation.http.HttpPipelineCallContextHelper;
import com.azure.core.implementation.jackson.ObjectMapperShim;
import com.azure.core.util.Context;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.UrlBuilder;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.logging.LogLevel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
Expand All @@ -37,7 +36,7 @@
* The pipeline policy that handles logging of HTTP requests and responses.
*/
public class HttpLoggingPolicy implements HttpPipelinePolicy {
private static final ObjectMapper PRETTY_PRINTER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
private static final ObjectMapperShim PRETTY_PRINTER = ObjectMapperShim.createPrettyPrintMapper();
private static final int MAX_BODY_LOG_SIZE = 1024 * 16;
private static final String REDACTED_PLACEHOLDER = "REDACTED";

Expand Down
Loading

0 comments on commit bf82b20

Please sign in to comment.