Skip to content

Commit bf82b20

Browse files
Liudmila Molkovasrnagar
andauthored
Shim ObjectMapper and add diagnostic version info to exceptions (Azure#23441)
* Shim for ObjectMapper and use shim within azure-core Co-authored-by: Srikanta <srnagar@microsoft.com>
1 parent 239a340 commit bf82b20

File tree

64 files changed

+1481
-502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1481
-502
lines changed

eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
<suppress checks="Header" files="com.azure.core.http.netty.implementation.HttpProxyHandler"/>
2424

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

3333
<!-- empty Enum Singleton Pattern -->
3434
<suppress checks="NoWhitespaceBefore" files="Messages.java"/>

eng/code-quality-reports/src/main/resources/spotbugs/spotbugs-exclude.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,10 +661,10 @@
661661
<!-- Serializers define non-transient non-serializable instance field -->
662662
<Match>
663663
<Or>
664-
<Class name="com.azure.core.util.serializer.FlatteningSerializer"/>
665-
<Class name="com.azure.core.util.serializer.FlatteningDeserializer"/>
666-
<Class name="com.azure.core.util.serializer.AdditionalPropertiesSerializer"/>
667-
<Class name="com.azure.core.util.serializer.AdditionalPropertiesDeserializer"/>
664+
<Class name="com.azure.core.implementation.jackson.FlatteningSerializer"/>
665+
<Class name="com.azure.core.implementation.jackson.FlatteningDeserializer"/>
666+
<Class name="com.azure.core.implementation.jackson.AdditionalPropertiesSerializer"/>
667+
<Class name="com.azure.core.implementation.jackson.AdditionalPropertiesDeserializer"/>
668668
</Or>
669669
<Bug pattern="SE_BAD_FIELD"/>
670670
</Match>

sdk/core/azure-core-jackson-tests/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Azure Core Jackson Tests is a test package that tests `azure-core` library again
44

55
## Getting started
66

7-
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.
7+
This package is intended to run in Live Test Azure Pipeline (`java - core - test`) under `jackson_supported_versions` and
8+
`jackson_unsupported_versions` test name), but you can run it locally by setting `AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION`
9+
or `AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION` environment variables.
810

911
## Key concepts
1012

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

1719
```powershell
18-
PS> $env:AZURE_CORE_TEST_JACKSON_VERSION="2.12.1"
20+
PS> $env:AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION="2.12.1"
1921
PS> mvn test
2022
```
2123

2224
Linux:
2325

2426
```bash
25-
$ export AZURE_CORE_TEST_JACKSON_VERSION="2.12.2"
27+
$ export AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION="2.12.2"
2628
$ mvn test
2729
```
2830

sdk/core/azure-core-jackson-tests/pom.xml

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<groupId>com.azure</groupId>
5656
<artifactId>azure-core</artifactId>
5757
<version>1.21.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core;current} -->
58+
<scope>test</scope>
5859
</dependency>
5960

6061
<dependency>
@@ -159,40 +160,34 @@
159160
</dependency>
160161
</dependencies>
161162

162-
<build>
163-
<plugins>
164-
<plugin>
165-
<groupId>org.apache.maven.plugins</groupId>
166-
<artifactId>maven-surefire-plugin</artifactId>
167-
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
168-
<configuration>
169-
<dependenciesToScan>
170-
<dependency>com.azure:azure-core</dependency>
171-
</dependenciesToScan>
172-
<excludes>
173-
<!-- TODO(limolkova) requires azure-core test resources-->
174-
<exclude>**/RestProxyXMLTests.java</exclude>
175-
</excludes>
176-
</configuration>
177-
</plugin>
178-
</plugins>
179-
</build>
180-
181163
<profiles>
182164
<profile>
183-
<id>java-lts</id>
165+
<id>default</id>
184166
<activation>
185-
<jdk>[11,)</jdk>
167+
<activeByDefault>true</activeByDefault>
186168
</activation>
169+
<build>
170+
<plugins>
171+
<plugin>
172+
<groupId>org.apache.maven.plugins</groupId>
173+
<artifactId>maven-surefire-plugin</artifactId>
174+
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
175+
<configuration>
176+
<skipTests>true</skipTests>
177+
</configuration>
178+
</plugin>
179+
</plugins>
180+
</build>
181+
187182
</profile>
188183

189184
<profile>
190-
<id>jackson-version-test-matrix</id>
185+
<id>jackson-supported-version-test-matrix</id>
191186
<activation>
192-
<property><name>env.AZURE_CORE_TEST_JACKSON_VERSION</name></property>
187+
<property><name>env.AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION</name></property>
193188
</activation>
194189
<properties>
195-
<jackson.version>${env.AZURE_CORE_TEST_JACKSON_VERSION}</jackson.version>
190+
<jackson.version>${env.AZURE_CORE_TEST_SUPPORTED_JACKSON_VERSION}</jackson.version>
196191
</properties>
197192
<dependencies>
198193
<!--Have to redeclare dependencies here to allow x-version-update script to work properly-->
@@ -226,8 +221,85 @@
226221
<version>${jackson.version}</version>
227222
<scope>test</scope>
228223
</dependency>
224+
</dependencies>
225+
<build>
226+
<plugins>
227+
<plugin>
228+
<groupId>org.apache.maven.plugins</groupId>
229+
<artifactId>maven-surefire-plugin</artifactId>
230+
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
231+
<configuration>
232+
<dependenciesToScan>
233+
<dependency>com.azure:azure-core</dependency>
234+
</dependenciesToScan>
235+
<excludes>
236+
<!-- TODO(limolkova) requires azure-core test resources-->
237+
<exclude>**/RestProxyXMLTests.java</exclude>
238+
<exclude>**/UnsupportedJacksonVersionsTests.java</exclude>
239+
</excludes>
240+
</configuration>
241+
</plugin>
242+
</plugins>
243+
</build>
244+
</profile>
229245

246+
<profile>
247+
<id>jackson-unsupported-version-test</id>
248+
<activation>
249+
<property><name>env.AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION</name></property>
250+
</activation>
251+
<properties>
252+
<jackson.version>${env.AZURE_CORE_TEST_UNSUPPORTED_JACKSON_VERSION}</jackson.version>
253+
</properties>
254+
<dependencies>
255+
<!--Have to redeclare dependencies here to allow x-version-update script to work properly-->
256+
<dependency>
257+
<groupId>com.fasterxml.jackson.core</groupId>
258+
<artifactId>jackson-annotations</artifactId>
259+
<version>${jackson.version}</version>
260+
<scope>test</scope>
261+
</dependency>
262+
<dependency>
263+
<groupId>com.fasterxml.jackson.core</groupId>
264+
<artifactId>jackson-core</artifactId>
265+
<version>${jackson.version}</version>
266+
<scope>test</scope>
267+
</dependency>
268+
<dependency>
269+
<groupId>com.fasterxml.jackson.core</groupId>
270+
<artifactId>jackson-databind</artifactId>
271+
<version>${jackson.version}</version>
272+
<scope>test</scope>
273+
</dependency>
274+
<dependency>
275+
<groupId>com.fasterxml.jackson.datatype</groupId>
276+
<artifactId>jackson-datatype-jsr310</artifactId>
277+
<version>${jackson.version}</version>
278+
<scope>test</scope>
279+
</dependency>
280+
<dependency>
281+
<groupId>com.fasterxml.jackson.dataformat</groupId>
282+
<artifactId>jackson-dataformat-xml</artifactId>
283+
<version>${jackson.version}</version>
284+
<scope>test</scope>
285+
</dependency>
230286
</dependencies>
287+
288+
<build>
289+
<plugins>
290+
<plugin>
291+
<groupId>org.apache.maven.plugins</groupId>
292+
<artifactId>maven-surefire-plugin</artifactId>
293+
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
294+
<configuration>
295+
<includes>
296+
<include>**/UnsupportedJacksonVersionsTests.java</include>
297+
</includes>
298+
</configuration>
299+
</plugin>
300+
</plugins>
301+
</build>
302+
231303
</profile>
232304
</profiles>
233305

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.implementation.jackson;
5+
6+
import com.azure.core.util.CoreUtils;
7+
import com.azure.core.util.serializer.JacksonAdapter;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.junit.jupiter.api.Test;
10+
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
13+
14+
public class UnsupportedJacksonVersionsTests {
15+
@Test
16+
public void testUnsupportedVersion() {
17+
String jacksonVersionString = ObjectMapper.class.getPackage().getImplementationVersion();
18+
String azureCoreVersion = CoreUtils
19+
.getProperties("azure-core.properties")
20+
.getOrDefault("version", null);
21+
22+
JacksonVersion version = JacksonVersion.getInstance();
23+
String helpInfo = version.getHelpInfo();
24+
assertTrue(helpInfo.contains("jackson-annotations=" + jacksonVersionString));
25+
assertTrue(helpInfo.contains("jackson-core=" + jacksonVersionString));
26+
assertTrue(helpInfo.contains("jackson-databind=" + jacksonVersionString));
27+
assertTrue(helpInfo.contains("jackson-dataformat-xml=" + jacksonVersionString));
28+
assertTrue(helpInfo.contains("jackson-datatype-jsr310=" + jacksonVersionString));
29+
assertTrue(helpInfo.contains("azure-core=" + azureCoreVersion));
30+
31+
Error error = assertThrows(Error.class, () -> new JacksonAdapter());
32+
assertTrue(error.getMessage().contains(helpInfo));
33+
}
34+
}

sdk/core/azure-core/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
--add-opens com.azure.core/com.azure.core.implementation.models.jsonflatten=com.fasterxml.jackson.databind
270270
--add-opens com.azure.core/com.azure.core.implementation.models.jsonflatten=ALL-UNNAMED
271271
--add-opens com.azure.core/com.azure.core.implementation.serializer=ALL-UNNAMED
272+
--add-opens com.azure.core/com.azure.core.implementation.jackson=ALL-UNNAMED
272273
--add-opens com.azure.core/com.azure.core.models=ALL-UNNAMED
273274
--add-opens com.azure.core/com.azure.core.util=ALL-UNNAMED
274275
--add-opens com.azure.core/com.azure.core.util.logging=ALL-UNNAMED

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLoggingPolicy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import com.azure.core.http.HttpRequest;
1212
import com.azure.core.http.HttpResponse;
1313
import com.azure.core.implementation.http.HttpPipelineCallContextHelper;
14+
import com.azure.core.implementation.jackson.ObjectMapperShim;
1415
import com.azure.core.util.Context;
1516
import com.azure.core.util.CoreUtils;
1617
import com.azure.core.util.UrlBuilder;
1718
import com.azure.core.util.logging.ClientLogger;
1819
import com.azure.core.util.logging.LogLevel;
19-
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import com.fasterxml.jackson.databind.SerializationFeature;
2120
import reactor.core.publisher.Mono;
2221

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

0 commit comments

Comments
 (0)