-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This PR solves the need to be able to customise the ObjectMapper in JSON based tests, e.g.: load in services. It also adds the option ALLOW_TRAILING_COMMA. Closes: #704, #740 PR: #741
- Loading branch information
1 parent
18ed594
commit c7bb969
Showing
17 changed files
with
544 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/junitpioneer/jupiter/json/DefaultObjectMapperProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016-2022 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class DefaultObjectMapperProvider implements ObjectMapperProvider { | ||
|
||
public DefaultObjectMapperProvider() { | ||
// recreate default constructor to prevent compiler warning | ||
} | ||
|
||
@Override | ||
public ObjectMapper get() { | ||
return new ObjectMapper(); | ||
} | ||
|
||
@Override | ||
public String id() { | ||
return "default"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/org/junitpioneer/jupiter/json/ObjectMapperProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2016-2022 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.json.JsonReadFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
/** | ||
* Service interface for providing a custom {@link com.fasterxml.jackson.databind.ObjectMapper} instance at runtime. | ||
* The default implementation doesn't register any additional Jackson modules. | ||
* | ||
* @see com.fasterxml.jackson.databind.Module | ||
*/ | ||
public interface ObjectMapperProvider { | ||
|
||
ObjectMapper get(); | ||
|
||
default ObjectMapper getLenient() { | ||
var mapper = get(); | ||
if (mapper instanceof JsonMapper) { | ||
return ((JsonMapper) mapper) | ||
.rebuild() | ||
.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES) | ||
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS) | ||
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES) | ||
.enable(JsonReadFeature.ALLOW_TRAILING_COMMA) | ||
.build(); | ||
} | ||
return get() | ||
.copyWith(JsonFactory | ||
.builder() | ||
.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES) | ||
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS) | ||
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES) | ||
.enable(JsonReadFeature.ALLOW_TRAILING_COMMA) | ||
.build()); | ||
} | ||
|
||
String id(); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/junitpioneer/jupiter/json/UseObjectMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2016-2022 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface UseObjectMapper { | ||
|
||
String value() default "default"; | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.junitpioneer.jupiter.json.ObjectMapperProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.junitpioneer.jupiter.json.DefaultObjectMapperProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.