Skip to content

Commit

Permalink
Detect YAML file extensions. See #15, #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersDJohnson committed Nov 21, 2015
1 parent cafe32d commit b238303
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
7 changes: 6 additions & 1 deletion jackson-json-reference-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand All @@ -196,7 +201,7 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.4.3</version>
<scope>test</scope>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,16 +86,35 @@ ObjectMapperFactory someMapperFactory() {

public JsonNode process(File file) throws JsonReferenceException, IOException {
JsonContext context = new JsonContext(file);
context.setFactory(someMapperFactory());
context.setFactory(getFactoryForFile(file));
return process(context);
}

public JsonNode process(URL url) throws JsonReferenceException, IOException {
JsonContext context = new JsonContext(url);
context.setFactory(someMapperFactory());
context.setFactory(getFactoryForFile(url));
return process(context);
}

public ObjectMapperFactory getFactoryForFile(File file) throws IOException {
return getFactoryForFile(file.getAbsolutePath());
}

public ObjectMapperFactory getFactoryForFile(URL url) throws IOException {
return getFactoryForFile(url.getPath());
}

public ObjectMapperFactory getFactoryForFile(String path) throws IOException {
// TOOD: Consider using file type detectors and MIME types.
// String contentType = Files.probeContentType(Paths.get(path));
// System.out.println(contentType);
String ext = FilenameUtils.getExtension(path);
if ("yml".equals(ext) || "yaml".equals(ext)) {
return new YamlObjectMapperFactory();
}
return DefaultObjectMapperFactory.instance;
}

public JsonNode process(JsonContext context, Set<JsonReference> processed) throws JsonReferenceException, IOException {
if (context.getFactory() == DefaultObjectMapperFactory.instance)
context.setFactory(someMapperFactory());
Expand Down Expand Up @@ -241,7 +261,7 @@ public JsonContext resolveFromContextToContext(JsonReference ref, JsonContext co
referencedContext = new JsonContext();
referencedContext.setUrl(absoluteReferencedUrl);
referencedContext.setNode(referencedNode);
referencedContext.setFactory(context.getFactory());
referencedContext.setFactory(getFactoryForFile(absoluteReferencedUrl));

return referencedContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

import static org.hamcrest.CoreMatchers.*;
Expand Down Expand Up @@ -42,6 +43,10 @@ private static File resourceAsFile(String path) {
return new File(resource(path));
}

private static URL resourceAsURL(String path) throws MalformedURLException {
return resourceAsFile(path).toURI().toURL();
}

@BeforeClass
public static void beforeClass() throws Exception {
server = new Server(8080);
Expand Down Expand Up @@ -222,6 +227,34 @@ public void testProcessYamlFileWithNestedMixedTypeScopes() throws IOException, J
assertThat(json, equalTo(expected));
}

@Test
public void testDetectYamlFile() throws JsonReferenceException, IOException {
File file = resourceAsFile("a.yaml");
String expected = "{\"a\":3}";

JsonReferenceProcessor processor = new JsonReferenceProcessor();
JsonNode node = processor.process(file);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(node);

assertThat(json, equalTo(expected));
}

@Test
public void testDetectYamlUrl() throws JsonReferenceException, IOException {
URL url = resourceAsURL("a.yaml");
String expected = "{\"a\":3}";

JsonReferenceProcessor processor = new JsonReferenceProcessor();
JsonNode node = processor.process(url);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(node);

assertThat(json, equalTo(expected));
}

@Test
public void testPreserveReferences() throws IOException, JsonReferenceException {

Expand Down

0 comments on commit b238303

Please sign in to comment.