Skip to content

Commit

Permalink
Allow schema exclusion to be package specific
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Oct 14, 2019
1 parent 28546f7 commit 361a536
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.trickl</groupId>
<artifactId>assertj-json-serialize</artifactId>
<version>0.1.8-SNAPSHOT</version>
<version>0.1.9-SNAPSHOT</version>
<packaging>jar</packaging>
<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;

import org.assertj.core.api.AbstractAssert;

Expand Down Expand Up @@ -45,7 +47,7 @@ public abstract class AbstractJsonObjectAssert<S extends AbstractJsonObjectAsser

private String projectDir = null;

private boolean noInlineSchemas = false;
private List<String> excludeInlineSchemaPackages = new ArrayList<>();

private String jsonDataFileExtension = DEFAULT_JSON_DATA_FILE_EXT;

Expand Down Expand Up @@ -184,8 +186,8 @@ public S doNotCreateExpectedIfAbsent() {
return myself;
}

public S withNoInlineSchemas() {
noInlineSchemas = true;
public S excludeInlineSchemaPackage(String packageName) {
excludeInlineSchemaPackages.add(packageName);
return myself;
}

Expand Down Expand Up @@ -218,8 +220,9 @@ protected String serialize(Object obj) {
protected String schema(Object obj) {
try {
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
if (noInlineSchemas) {
visitor.setVisitorContext(new NoInlineSchemaVisitorContext());
if (excludeInlineSchemaPackages.size() > 0) {
visitor.setVisitorContext(
new ExcludeInlineSchemaVisitorContext(excludeInlineSchemaPackages));
}
objectMapper.acceptJsonFormatVisitor(actual.getObject().getClass(), visitor);
JsonSchema schema = visitor.finalSchema();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.trickl.assertj.core.api.json.serialize;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.module.jsonSchema.factories.VisitorContext;

import java.util.List;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ExcludeInlineSchemaVisitorContext extends VisitorContext {
private final List<String> excludePackages;

@Override
public String getSeenSchemaUri(JavaType javaType) {
if (javaType != null && !javaType.isPrimitive() && isInExcludedPackage(javaType)) {
return javaTypeToUrn(javaType);
}
return null;
}

private boolean isInExcludedPackage(JavaType javaType) {
String packageName = javaType.getRawClass().getPackage().getName();
return excludePackages.stream().anyMatch((exclude ->
packageName.equals(exclude) || packageName.startsWith(exclude + ".")
));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import com.trickl.assertj.examples.Example;
import com.trickl.assertj.examples.NestedComplexExample;
import com.trickl.assertj.examples.NestedExample;

import org.junit.Test;
Expand Down Expand Up @@ -72,10 +73,10 @@ public void should_pass_on_schema_match() {
}

@Test
public void should_respect_no_inlining() {
public void should_respect_no_inlining_simple() {
NestedExample nested = new NestedExample();
assertThat(nested)
.withNoInlineSchemas()
.excludeInlineSchemaPackage("com.trickl.assertj.examples")
.schemaAsExpected();
}

Expand All @@ -86,4 +87,12 @@ public void should_respect_allow_inlining() {
.withSchemaFileExtension(".schema2.json")
.schemaAsExpected();
}

@Test
public void should_respect_no_inlining_complex() {
NestedComplexExample nested = new NestedComplexExample();
assertThat(nested)
.excludeInlineSchemaPackage("com.trickl.assertj.examples")
.schemaAsExpected();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.trickl.assertj.examples;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;
import java.util.Map;

import lombok.Data;

@Data
public class NestedComplexExample {
@JsonProperty("list-example")
private List<Example> firstExample;

@JsonProperty("map-example")
private Map<String, Example> secondExample;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "urn:jsonschema:com:trickl:assertj:examples:NestedComplexExample",
"type": "object",
"properties": {
"list-example": {
"type": "array",
"items": {
"type": "object",
"$ref": "urn:jsonschema:com:trickl:assertj:examples:Example"
}
},
"map-example": {
"additionalProperties": {
"type": "object",
"$ref": "urn:jsonschema:com:trickl:assertj:examples:Example"
},
"type": "object"
}
}
}

0 comments on commit 361a536

Please sign in to comment.