Skip to content

Commit

Permalink
Merge pull request #76 from idelvall/master
Browse files Browse the repository at this point in the history
Make Object schema properties retain order (wrt @JsonPropertyOrder etc) (see #27)
  • Loading branch information
cowtowncoder committed Sep 11, 2015
2 parents 22098c5 + d273183 commit 2106546
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.module.jsonSchema.types;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -12,6 +11,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import java.util.LinkedHashMap;

/**
* This type represents a {@link JsonSchema} as an object type
Expand Down Expand Up @@ -68,8 +68,8 @@ public class ObjectSchema extends ContainerTypeSchema
public ObjectSchema()
{
dependencies = new ArrayList<Dependency>();
patternProperties = new HashMap<String, JsonSchema>();
properties = new HashMap<String, JsonSchema>();
patternProperties = new LinkedHashMap<String, JsonSchema>();
properties = new LinkedHashMap<String, JsonSchema>();
}

public boolean addSchemaDependency(String depender, JsonSchema parentMustMatch) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.fasterxml.jackson.module.jsonSchema;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;

public class TestCyclic extends SchemaTestBase
{
// [Issue#4]
@JsonPropertyOrder({"next", "name"})
public class Loop {
public String name;
public Loop next;
Expand Down Expand Up @@ -42,6 +44,9 @@ public void testSimpleCyclic() throws Exception
"\"properties\":{\"next\":{\"type\":\"object\"," +
"\"$ref\":\"urn:jsonschema:com:fasterxml:jackson:module:jsonSchema:TestCyclic:Loop\"}" +
",\"name\":{\"type\":\"string\"}}}";

System.out.println(EXP);
System.out.println(json);

assertEquals(aposToQuotes(EXP), json);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.fasterxml.jackson.module.jsonSchema;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MapperFeature;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestPropertyOrderInSchema extends SchemaTestBase {

@JsonPropertyOrder({"c", "b", "a"})
static class Bean {

private int b;
private String c;
private String a;

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}
}

@JsonPropertyOrder(alphabetic = true)
static class Bean2 {

private int b;
private String c;
private String a;

public int getB() {
return b;
}

public void setB(int b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/


public void testAnnotationOrder() throws Exception {
ObjectMapper MAPPER = objectMapper();
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(Bean.class);
assertEquals(jsonSchema.asObjectSchema().getProperties().keySet().toString(), "[c, b, a]");
}

public void testAlphabeticOrder() throws Exception {
final ObjectMapper MAPPER = objectMapper();
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(Bean2.class);
assertEquals(jsonSchema.asObjectSchema().getProperties().keySet().toString(), "[a, b, c]");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ enum SchemaEnum {
@JsonPropertyOrder(alphabetic=true)
static class SchemableBasic
{
public SchemaEnum testEnum;
public String name;
public JsonSerializable someSerializable;
}
Expand Down

0 comments on commit 2106546

Please sign in to comment.