-
Notifications
You must be signed in to change notification settings - Fork 137
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
Refactoring: Validation Groups, v3 and v4 Required support, Chaining of processors #125
base: master
Are you sure you want to change the base?
Changes from 6 commits
58e832f
52c5d9b
3f8345a
c215c9c
fe7045f
3a11d6a
efa3742
6fe885b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ target | |
*.ipr | ||
*.iws | ||
.idea | ||
/bin/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
package com.fasterxml.jackson.module.jsonSchema; | ||
|
||
import com.fasterxml.jackson.annotation.*; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo.As; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; | ||
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; | ||
import com.fasterxml.jackson.module.jsonSchema.types.*; | ||
import com.fasterxml.jackson.module.jsonSchema.factories.WrapperFactory.JsonSchemaVersion; | ||
import com.fasterxml.jackson.module.jsonSchema.types.AnySchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.ArraySchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.BooleanSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.ContainerTypeSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.IntegerSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.NullSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.NumberSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.SimpleTypeSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.UnionTypeSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.types.ValueTypeSchema; | ||
|
||
/** | ||
* The type wraps the json schema specification at : | ||
|
@@ -33,7 +48,7 @@ | |
* "id":{ | ||
* "type":"number", | ||
* "description":"Product identifier", | ||
* "required":true | ||
* "required":true | ||
* }, | ||
* "name":{ | ||
* "description":"Name of the product", | ||
|
@@ -73,6 +88,21 @@ | |
@JsonTypeIdResolver(JsonSchemaIdResolver.class) | ||
public abstract class JsonSchema | ||
{ | ||
@JsonIgnore | ||
protected JsonSchemaVersion version; | ||
|
||
protected JsonSchema() { | ||
//jackson deserialization only | ||
} | ||
|
||
protected JsonSchema(JsonSchemaVersion version) { | ||
this.version = version; | ||
} | ||
|
||
protected JsonSchema(JsonSchemaVersion version, boolean set$Schema) { | ||
this.version = version; | ||
} | ||
|
||
/** | ||
* This attribute defines the current URI of this schema (this attribute is | ||
* effectively a "self" link). This URI MAY be relative or absolute. If the | ||
|
@@ -141,12 +171,16 @@ public abstract class JsonSchema | |
*/ | ||
private JsonSchema[] extendsextends; | ||
|
||
/** | ||
* This attribute indicates if the instance must have a value, and not be | ||
* undefined. This is false by default, making the instance optional. | ||
*/ | ||
@JsonProperty | ||
private Boolean required = null; | ||
/** | ||
* This attribute indicates if the instance must have a value, and not be | ||
* undefined. This is false by default, making the instance optional. | ||
* Available in Draft V3 spec ONLY. | ||
* | ||
* @deprecated Since 2.9 - Use setRequired on ObjectSchema from Draft V4 onwards. | ||
*/ | ||
@Deprecated | ||
@JsonProperty("required") | ||
private Boolean requiredBoolean = null; | ||
|
||
/** | ||
* This attribute indicates if the instance is not modifiable. | ||
|
@@ -161,8 +195,6 @@ public abstract class JsonSchema | |
*/ | ||
private String description; | ||
|
||
protected JsonSchema() { } | ||
|
||
/** | ||
* Attempt to return this JsonSchema as an {@link AnySchema} | ||
* @return this as an AnySchema if possible, or null otherwise | ||
|
@@ -279,7 +311,11 @@ public ValueTypeSchema asValueTypeSchema() { | |
return null; | ||
} | ||
|
||
public String getId() { | ||
public JsonSchemaVersion getVersion() { | ||
return version; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
|
@@ -299,8 +335,8 @@ public JsonSchema[] getExtends() { | |
return extendsextends; | ||
} | ||
|
||
public Boolean getRequired() { | ||
return required; | ||
public Boolean getRequiredBoolean() { | ||
return requiredBoolean; | ||
} | ||
|
||
public Boolean getReadonly() { | ||
|
@@ -441,6 +477,9 @@ public boolean isValueTypeSchema() { | |
|
||
public void set$schema(String $schema) { | ||
this.$schema = $schema; | ||
if (version == null) { | ||
this.version = JsonSchemaVersion.fromSchemaString($schema).orElse(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why doesn't method just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that version isn't part of the JsonSchema so it is annotated with JsonIgnore. So when deserializing the version isn't set. So what this is doing is deriving the version from the schema. Thoughts on a better way to do this? Perhaps with a custom deserializer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fromSchemaString returns an Optional so that you can set it to a default value if you wanted. IE |
||
} | ||
} | ||
|
||
public void setDisallow(JsonSchema[] disallow) { | ||
|
@@ -455,8 +494,11 @@ public void setId(String id) { | |
this.id = id; | ||
} | ||
|
||
public void setRequired(Boolean required) { | ||
this.required = required; | ||
public void setRequiredBoolean(Boolean requiredBoolean) { | ||
if (!JsonSchemaVersion.DRAFT_V3.equals(version)) { | ||
throw new RuntimeException("You can only set the required boolean on Draft V3. You have: " + version); | ||
} | ||
this.requiredBoolean = requiredBoolean; | ||
} | ||
|
||
public void setReadonly(Boolean readonly){ | ||
|
@@ -478,33 +520,34 @@ public void enrichWithBeanProperty(BeanProperty beanProperty) { | |
} | ||
|
||
/** | ||
* Create a schema which verifies only that an object is of the given format. | ||
* @param format the format to expect | ||
* @return the schema verifying the given format | ||
*/ | ||
public static JsonSchema minimalForFormat(JsonFormatTypes format) | ||
* Create a schema which verifies only that an object is of the given format. | ||
* @param jsonVersion | ||
* @param format the format to expect | ||
* @return the schema verifying the given format | ||
*/ | ||
public static JsonSchema minimalForFormat(JsonSchemaVersion jsonVersion, JsonFormatTypes format) | ||
{ | ||
if (format != null) { | ||
switch (format) { | ||
case ARRAY: | ||
return new ArraySchema(); | ||
return new ArraySchema(jsonVersion); | ||
case OBJECT: | ||
return new ObjectSchema(); | ||
return new ObjectSchema(jsonVersion); | ||
case BOOLEAN: | ||
return new BooleanSchema(); | ||
return new BooleanSchema(jsonVersion); | ||
case INTEGER: | ||
return new IntegerSchema(); | ||
return new IntegerSchema(jsonVersion); | ||
case NUMBER: | ||
return new NumberSchema(); | ||
return new NumberSchema(jsonVersion); | ||
case STRING: | ||
return new StringSchema(); | ||
return new StringSchema(jsonVersion); | ||
case NULL: | ||
return new NullSchema(); | ||
return new NullSchema(jsonVersion); | ||
case ANY: | ||
default: | ||
} | ||
} | ||
return new AnySchema(); | ||
return new AnySchema(jsonVersion); | ||
} | ||
|
||
@Override | ||
|
@@ -522,7 +565,7 @@ protected boolean _equals(JsonSchema that) | |
|
||
// 27-Apr-2015, tatu: Should not need to check type explicitly | ||
// && equals(getType(), getType()) | ||
&& equals(getRequired(), that.getRequired()) | ||
&& ((JsonSchemaVersion.DRAFT_V3.equals(version)) ? equals(getRequiredBoolean(), that.getRequiredBoolean()) : true) | ||
&& equals(getReadonly(), that.getReadonly()) | ||
&& equals(get$ref(), that.get$ref()) | ||
&& equals(get$schema(), that.get$schema()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fasterxml.jackson.module.jsonSchema.annotation; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ METHOD, FIELD, PARAMETER, TYPE }) | ||
@Retention(RUNTIME) | ||
public @interface JsonSchemaTitle { | ||
String value(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would the type be included in accessor name? Is that for schema v4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I added the v4 version of required into the ObjectSchema I couldn't have 2 getRequired methods retuning different types. So I changed this one and that is the best I came up with at the time. I probably should put this back to getRequired and rename the one in ObjectSchema to requiredFields and annotate it to be @JsonProperty("required"). Thoughts?