DATAREST-354 - General rewrite of the JSONSchema support.
Significant overhaul of the JSONSchema support. This currently adds the following features: - Complex nested types are exposed as descriptors with the properties pointing to them whenever necessary. - Sets are treated as unique collections. - Enums are handled as expected (enum values are listed). - Renamings via @JsonProperty are considered. - @JsonProperty(required = true) is considered and added to required properties. - Date/time types (legacy Date, JSR-310, ThreeTenBP and Joda Time) are exposed with format "date-time". - Objects with @JsonValue methods are considered to be rendered as String value. - Formats and patterns can be manually configured on MetadataConfiguration. TODOs: - Implementation polish, JavaDoc - Automatically inspect ObjectMapper to detect customizations made through Mixins and custom Serializers.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.config;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
/**
|
||||
* An enum to represent JSON Schema pre-defined formats.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 2.3
|
||||
*/
|
||||
public enum JsonSchemaFormat {
|
||||
|
||||
EMAIL, DATE_TIME, HOSTNAME, IPV4, IPV6, URI;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Enum#toString()
|
||||
*/
|
||||
@JsonValue
|
||||
public String toString() {
|
||||
return name().toLowerCase(Locale.US).replaceAll("_", "-");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Configuration for metadata exposure.
|
||||
*
|
||||
@@ -22,6 +29,8 @@ package org.springframework.data.rest.core.config;
|
||||
*/
|
||||
public class MetadataConfiguration {
|
||||
|
||||
private final Map<Class<?>, JsonSchemaFormat> schemaFormats = new HashMap<Class<?>, JsonSchemaFormat>();
|
||||
private final Map<Class<?>, Pattern> patterns = new HashMap<Class<?>, Pattern>();
|
||||
private boolean omitUnresolvableDescriptionKeys = true;
|
||||
private boolean alpsEnabled = true;
|
||||
|
||||
@@ -63,4 +72,56 @@ public class MetadataConfiguration {
|
||||
public boolean alpsEnabled() {
|
||||
return alpsEnabled;
|
||||
}
|
||||
|
||||
public void registerJsonSchemaFormat(JsonSchemaFormat format, Class<?>... types) {
|
||||
|
||||
Assert.notNull(format, "JsonSchemaFormat must not be null!");
|
||||
|
||||
for (Class<?> type : types) {
|
||||
schemaFormats.put(type, format);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link JsonSchemaFormat} to be used for the given type.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public JsonSchemaFormat getSchemaFormatFor(Class<?> type) {
|
||||
return schemaFormats.get(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the given formatting patter for the given value type.
|
||||
*
|
||||
* @param pattern must not be {@literal null} or empty.
|
||||
* @param type must not be {@literal null}.
|
||||
*/
|
||||
public void registerFormattingPatternFor(String pattern, Class<?> type) {
|
||||
|
||||
Assert.hasText(pattern, "Pattern must not be null or empty!");
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
this.patterns.put(type, Pattern.compile(pattern));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Pattern} registered for the given value type.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public Pattern getPatternFor(Class<?> type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
for (Entry<Class<?>, Pattern> entry : this.patterns.entrySet()) {
|
||||
if (entry.getKey().isAssignableFrom(type)) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return this.patterns.get(type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.domain.jpa;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -5,6 +20,7 @@ import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
@@ -15,6 +31,7 @@ import javax.persistence.PrePersist;
|
||||
* An entity that represents a person.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
Reference in New Issue
Block a user