Skip to content

Make Object schema properties retain order (wrt @JsonPropertyOrder etc) (see #27) #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this removal intentional?

public JsonSerializable someSerializable;
}
Expand Down