-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
129565e
commit 671188e
Showing
5 changed files
with
130 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/com/fasterxml/jackson/module/jsonSchema/CustomSchemaReadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.fasterxml.jackson.module.jsonSchema; | ||
|
||
import com.fasterxml.jackson.databind.DatabindContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; | ||
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema; | ||
|
||
public class CustomSchemaReadTest extends SchemaTestBase | ||
{ | ||
@JsonTypeIdResolver(MyResolver.class) | ||
public static class MySchema extends ObjectSchema { | ||
} | ||
|
||
static class MyResolver extends JsonSchemaIdResolver | ||
{ | ||
@Override | ||
public String idFromValue(Object value) { | ||
if (value instanceof MySchema) { | ||
return "CUSTOM"; | ||
} | ||
return super.idFromValue(value); | ||
} | ||
|
||
@Override | ||
public String idFromValueAndType(Object value, Class<?> suggestedType) { | ||
if (value instanceof MySchema) { | ||
return "CUSTOM"; | ||
} | ||
return super.idFromValueAndType(value, suggestedType); | ||
} | ||
|
||
@Override | ||
public JavaType typeFromId(DatabindContext ctxt, String id) { | ||
if ("CUSTOM".equals(id)) { | ||
return ctxt.constructType(MySchema.class); | ||
} | ||
return super.typeFromId(ctxt, id); | ||
} | ||
} | ||
|
||
// [module-jsonSchema#67] | ||
public void testSchema() throws Exception | ||
{ | ||
String input = "{ \"type\" : \"CUSTOM\" , \"id\" : \"7a2e8538-196b-423e-b714-13515848ec0c\" , \"description\" : \"My Schema\" , \"title\" : \"my-json-schema\" , \"properties\" : { \"myarray\" : { \"type\" : \"array\" , \"required\" : true , \"title\" : \"my property #2\" , \"items\" : { \"type\" : \"string\"} , \"maxItems\" : 5} , \"mystring\" : { \"type\" : \"string\" , \"required\" : true , \"title\" : \"my property #1\" , \"format\" : \"REGEX\" , \"pattern\" : \"\\\\w+\"} , \"myobject\" : { \"type\" : \"object\" , \"required\" : true , \"title\" : \"my property #3\" , \"properties\" : { \"subprop\" : { \"type\" : \"string\" , \"required\" : true , \"title\" : \"sub property #1\" , \"format\" : \"REGEX\" , \"pattern\" : \"\\\\w{3}\"}}}}}"; | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
// ObjectSchema schema = mapper.readValue(json, ObjectSchema.class); // works | ||
MySchema schema = mapper.readValue(input, MySchema.class); // fails | ||
|
||
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema); | ||
assertNotNull(json); | ||
} | ||
} |