Skip to content

Commit

Permalink
Fix #47
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 21, 2014
1 parent 10692e8 commit d924e7f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 22 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ Anton O (mavarazy@github)

#35: Add annotation based support for HyperSchema to JsonSchema
(2.4.1)

Greg Adams (gadams00@github)

#47: VisitorContext results in incomplete json schema output
(2.4.4)
16 changes: 11 additions & 5 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
Project: jackson-module-jsonSchema
Version: 2.4.3 (03-Oct-2014)

#34: NPE when generating schema for class with JsonValue annotation over Collection/Array
(reported by peshitz@github)

------------------------------------------------------------------------
=== History: ===
=== Releases ===
------------------------------------------------------------------------

2.4.4 (not yet released)

#47: VisitorContext results in incomplete json schema output
(reported by Greg A)

2.4.3 (03-Oct-2014)

#34: NPE when generating schema for class with JsonValue annotation over Collection/Array
(reported by peshitz@github)

2.4.2 (14-Aug-2014)

No changes since 2.4.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected JsonSchema propertySchema(JsonFormatVisitable handler, JavaType proper

// check if we've seen this sub-schema already and return a reference-schema if we have
if (visitorContext != null) {
String seenSchemaUri = VisitorContext.getSeenSchemaUri(propertyTypeHint);
String seenSchemaUri = visitorContext.getSeenSchemaUri(propertyTypeHint);
if (seenSchemaUri != null) {
return new ReferenceSchema(seenSchemaUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected JsonSchema propertySchema(BeanProperty prop)
}

// check if we've seen this argument's sub-schema already and return a reference-schema if we have
String seenSchemaUri = VisitorContext.getSeenSchemaUri(prop.getType());
String seenSchemaUri = visitorContext.getSeenSchemaUri(prop.getType());
if (seenSchemaUri != null) {
return new ReferenceSchema(seenSchemaUri);
}
Expand All @@ -125,7 +125,7 @@ protected JsonSchema propertySchema(JsonFormatVisitable handler, JavaType proper
{
// check if we've seen this argument's sub-schema already and return a reference-schema if we have
if (visitorContext != null) {
String seenSchemaUri = VisitorContext.getSeenSchemaUri(propertyTypeHint);
String seenSchemaUri = visitorContext.getSeenSchemaUri(propertyTypeHint);
if (seenSchemaUri != null) {
return new ReferenceSchema(seenSchemaUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public JsonObjectFormatVisitor expectObjectFormat(JavaType convertedType) {
schema = s;

// if we don't already have a recursive visitor context, create one
if (visitorContext == null)
{
if (visitorContext == null) {
visitorContext = new VisitorContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@

import java.util.HashSet;

/**
* Created by adb on 6/9/14.
*/
public class VisitorContext {
public class VisitorContext
{
private final HashSet<JavaType> seenSchemas = new HashSet<JavaType>();

private static HashSet<JavaType> seenSchemas = new HashSet<JavaType>();

static public String addSeenSchemaUri(JavaType aSeenSchema)
{
public String addSeenSchemaUri(JavaType aSeenSchema) {
if (aSeenSchema != null && !aSeenSchema.isPrimitive()) {
seenSchemas.add(aSeenSchema);
return javaTypeToUrn(aSeenSchema);
}
return null;
}

static public String getSeenSchemaUri(JavaType aSeenSchema)
{
public String getSeenSchemaUri(JavaType aSeenSchema) {
return (seenSchemas.contains(aSeenSchema)) ? javaTypeToUrn(aSeenSchema) : null;
}

static public String javaTypeToUrn(JavaType jt)
{
public String javaTypeToUrn(JavaType jt) {
return "urn:jsonschema:" + jt.toCanonical().replace('.', ':').replace('$', ':');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.fasterxml.jackson.module.jsonSchema;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class TestVisitorContext extends SchemaTestBase
{
static class Foo {
private String fooProp1;
private int fooProp2;
private Bar fooProp3;

public String getFooProp1() {
return fooProp1;
}

public void setFooProp1(String fooProp1) {
this.fooProp1 = fooProp1;
}

public int getFooProp2() {
return fooProp2;
}

public void setFooProp2(int fooProp2) {
this.fooProp2 = fooProp2;
}

public Bar getFooProp3() {
return fooProp3;
}

public void setFooProp3(Bar fooProp3) {
this.fooProp3 = fooProp3;
}
}

static class Bar {
private String barProp1;
private int barProp2;

public String getBarProp1() {
return barProp1;
}

public void setBarProp1(String barProp1) {
this.barProp1 = barProp1;
}

public int getBarProp2() {
return barProp2;
}

public void setBarProp2(int barProp2) {
this.barProp2 = barProp2;
}
}

static class Qwer {
private String qwerProp1;
private Bar qwerProp2;

public String getQwerProp1() {
return qwerProp1;
}

public void setQwerProp1(String qwerProp1) {
this.qwerProp1 = qwerProp1;
}

public Bar getQwerProp2() {
return qwerProp2;
}

public void setQwerProp2(Bar qwerProp2) {
this.qwerProp2 = qwerProp2;
}
}

// for [jsonSchema#47]
public void testSchemaGeneration() throws Exception {
String schemaStr = generateSchema(Foo.class);
schemaStr = generateSchema(Qwer.class);
// ok, not very robust but has to do:
if (schemaStr.indexOf("$ref") >= 0) {
fail("Should not have $ref for type Bar (as per issue #47): "+schemaStr);
}
}

private final static ObjectMapper MAPPER = new ObjectMapper();

private String generateSchema(Class<?> clazz) throws Exception
{
MAPPER.configure(SerializationFeature.INDENT_OUTPUT, true);
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
MAPPER.acceptJsonFormatVisitor(MAPPER.constructType(clazz), visitor);
return MAPPER.writeValueAsString(visitor.finalSchema());
}

}

0 comments on commit d924e7f

Please sign in to comment.