From 92517a491f79a5ebe8cfb21cea58b9ab51fb65e1 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 11:13:23 -0400 Subject: [PATCH 1/7] Short-circuit identifier sanitization. Do not bother sanitizing each character if the full string is valid. --- .../src/main/java/org/jsonschema2pojo/util/NameHelper.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java index 19f990a86..16fc12a11 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java @@ -22,6 +22,7 @@ import org.apache.commons.lang3.text.WordUtils; import org.jsonschema2pojo.GenerationConfig; +import static javax.lang.model.SourceVersion.isIdentifier; import static javax.lang.model.SourceVersion.isKeyword; import static org.apache.commons.lang3.StringUtils.capitalize; import static org.apache.commons.lang3.StringUtils.containsAny; @@ -36,6 +37,10 @@ public NameHelper(GenerationConfig generationConfig) { } public static String replaceIllegalCharacters(String name) { + if (isIdentifier(name)) { + return name; + } + for (char character: name.toCharArray()) { if (!Character.isJavaIdentifierPart(character)) { name = name.replace(String.valueOf(character), "_"); From 7c385263773c6f582914c7e5f811488f5563685b Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 11:24:15 -0400 Subject: [PATCH 2/7] Add annotation for field format --- .../org/jsonschema2pojo/annotations/FieldFormat.java | 12 ++++++++++++ .../jsonschema2pojo/annotations/FieldFormats.java | 10 ++++++++++ .../org/jsonschema2pojo/annotations/FieldTypes.java | 2 ++ 3 files changed, 24 insertions(+) create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java new file mode 100644 index 000000000..edbca976c --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java @@ -0,0 +1,12 @@ +package org.jsonschema2pojo.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface FieldFormat { + FieldFormats value(); +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java new file mode 100644 index 000000000..da6a54952 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java @@ -0,0 +1,10 @@ +package org.jsonschema2pojo.annotations; + +/** + * For annotating with JSON schema field formats, as used by json-editor. + * + * Created by kat on 6/22/16. + */ +public enum FieldFormats { + color, date, datetime, datetimelocal, email, month, number, range, tel, text, textarea, time, url, week +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java index e2ef0d7f5..4749fd620 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java @@ -17,6 +17,8 @@ package org.jsonschema2pojo.annotations; /** + * For annotating with JSON schema field types. + * * Created by kat on 11/22/15. */ public enum FieldTypes { From 08b372609de7a00929854a36c8d794f24d7c201e Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 11:26:10 -0400 Subject: [PATCH 3/7] Remove unnecessary null check. IntelliJ is certain it will never be null. --- .../jsonschema2pojo/JsonEditorAnnotator.java | 76 +++++++++---------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java index d58c55727..f8a620184 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java @@ -95,54 +95,50 @@ public void propertyField(JFieldVar field, JDefinedClass clazz, if (propertyNode.has("fieldType")) { String fieldType = propertyNode.get("fieldType").asText(); FieldTypes foundFieldType = FieldTypes.valueOf(fieldType); - - if (foundFieldType != null) { - JAnnotationUse fieldTypeAnnotation = field.annotate(FieldType.class); - fieldTypeAnnotation.param("value", foundFieldType); - - /* Additional annotations for reference types. To see how they're built in DRIVER: - * https://github.com/WorldBank-Transport/DRIVER/blob/develop/schema_editor/app/scripts/schemas/schemas-service.js - * NB: in DRIVER usage of json-editor, referenced field is always watch.target, - * enumSource value is always _localId, and id is always the default 'item'. - */ - if (foundFieldType == FieldTypes.reference) { - - // annotate with watch target (referenced field) - JsonNode watch = propertyNode.get("watch"); - if (watch != null && watch.has("target")) { - String target = watch.get("target").asText(); - if (target != null) { - JAnnotationUse watchTargetAnnotation = field.annotate(WatchTarget.class); - watchTargetAnnotation.param("value", target); - } else { - Log.warning("No watch target found for reference type field " + propertyName); - } + + JAnnotationUse fieldTypeAnnotation = field.annotate(FieldType.class); + fieldTypeAnnotation.param("value", foundFieldType); + + /* Additional annotations for reference types. To see how they're built in DRIVER: + * https://github.com/WorldBank-Transport/DRIVER/blob/develop/schema_editor/app/scripts/schemas/schemas-service.js + * NB: in DRIVER usage of json-editor, referenced field is always watch.target, + * enumSource value is always _localId, and id is always the default 'item'. + */ + if (foundFieldType == FieldTypes.reference) { + + // annotate with watch target (referenced field) + JsonNode watch = propertyNode.get("watch"); + if (watch != null && watch.has("target")) { + String target = watch.get("target").asText(); + if (target != null) { + JAnnotationUse watchTargetAnnotation = field.annotate(WatchTarget.class); + watchTargetAnnotation.param("value", target); } else { - Log.warning("No watch/target found on reference type field " + propertyName); + Log.warning("No watch target found for reference type field " + propertyName); } + } else { + Log.warning("No watch/target found on reference type field " + propertyName); + } - // annotate with label template to use from referenced field - JsonNode enumSources = propertyNode.get("enumSource"); - if (enumSources != null && enumSources.isArray()) { - // expect DRIVER enumSources to be a list with single element - JsonNode enumSource = enumSources.get(0); - if (enumSource != null && enumSource.has("title")) { - String titlePattern = enumSource.get("title").asText(); - if (titlePattern != null) { - JAnnotationUse titlePatternAnnotation = field.annotate(ReferenceTitlePattern.class); - titlePatternAnnotation.param("value", titlePattern); - } else { - Log.info("No title pattern set for reference type field " + propertyName); - } + // annotate with label template to use from referenced field + JsonNode enumSources = propertyNode.get("enumSource"); + if (enumSources != null && enumSources.isArray()) { + // expect DRIVER enumSources to be a list with single element + JsonNode enumSource = enumSources.get(0); + if (enumSource != null && enumSource.has("title")) { + String titlePattern = enumSource.get("title").asText(); + if (titlePattern != null) { + JAnnotationUse titlePatternAnnotation = field.annotate(ReferenceTitlePattern.class); + titlePatternAnnotation.param("value", titlePattern); } else { - Log.warning("No enumSource/title found for reference type field " + propertyName); + Log.info("No title pattern set for reference type field " + propertyName); } } else { - Log.warning("No enumSources found for reference type field " + propertyName); + Log.warning("No enumSource/title found for reference type field " + propertyName); } + } else { + Log.warning("No enumSources found for reference type field " + propertyName); } - } else { - Log.info("No field type found for " + fieldType); } } From 969929187bbc11dd1c05be4dcbe9dfe1ad330941 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 11:47:29 -0400 Subject: [PATCH 4/7] Add other field types and formats from json-editor README --- .../java/org/jsonschema2pojo/annotations/FieldFormats.java | 5 ++++- .../java/org/jsonschema2pojo/annotations/FieldTypes.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java index da6a54952..2a721ec74 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java @@ -6,5 +6,8 @@ * Created by kat on 6/22/16. */ public enum FieldFormats { - color, date, datetime, datetimelocal, email, month, number, range, tel, text, textarea, time, url, week + // text type formats + color, date, datetime, datetimelocal, email, html, month, number, range, tel, text, textarea, time, url, week, + // other type formats + checkbox, table, select, grid, location } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java index 4749fd620..df8e78f14 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java @@ -22,5 +22,5 @@ * Created by kat on 11/22/15. */ public enum FieldTypes { - text, image, selectlist, reference + text, image, selectlist, reference, integer, string, object, booleantype, arraytype } From 3afb48c045de93a207c751873cb4ef601486e1b0 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 13:02:33 -0400 Subject: [PATCH 5/7] Add field type annotation to output --- .../java/org/jsonschema2pojo/JsonEditorAnnotator.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java index f8a620184..3a526e335 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java @@ -142,6 +142,16 @@ public void propertyField(JFieldVar field, JDefinedClass clazz, } } + if (propertyNode.has("format")) { + String format = propertyNode.get("format").asText(); + FieldFormats foundFormat = FieldFormats.valueOf(format); + + JAnnotationUse fieldFormatAnnotation = field.annotate(FieldFormat.class); + if (!format.isEmpty()) { + fieldFormatAnnotation.param("value", foundFormat); + } + } + if (propertyNode.has("options")) { JsonNode options = propertyNode.get("options"); if (options.has("hidden")) { From 73838b9155dc3ea9dcd701ecc22c6f074f25c4dd Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 13:19:33 -0400 Subject: [PATCH 6/7] Handle numeric types specified in field format. For non-standard usage with string type. --- .../annotations/FieldFormats.java | 2 +- .../org/jsonschema2pojo/rules/FormatRule.java | 62 ++++++++++++++----- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java index 2a721ec74..2b36683c7 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java @@ -7,7 +7,7 @@ */ public enum FieldFormats { // text type formats - color, date, datetime, datetimelocal, email, html, month, number, range, tel, text, textarea, time, url, week, + color, date, datetime, datetimelocal, email, html, month, integer, number, range, tel, text, textarea, time, url, week, // other type formats checkbox, table, select, grid, location } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/FormatRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/FormatRule.java index 4eda1e3a8..ebab04784 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/FormatRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/FormatRule.java @@ -16,11 +16,13 @@ package org.jsonschema2pojo.rules; +import java.math.BigDecimal; import java.net.URI; import java.util.Date; import java.util.UUID; import java.util.regex.Pattern; +import com.sun.codemodel.JCodeModel; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.LocalTime; @@ -81,48 +83,53 @@ protected FormatRule(RuleFactory ruleFactory) { */ @Override public JType apply(String nodeName, JsonNode node, JType baseType, Schema schema) { + + String nodeText = node.asText(); - if (node.asText().equals("date-time")) { + if (nodeText.equals("date-time")) { return baseType.owner().ref(getDateTimeType()); - } else if (node.asText().equals("date")) { + } else if (nodeText.equals("date")) { return baseType.owner().ref(getDateOnlyType()); - } else if (node.asText().equals("time")) { + } else if (nodeText.equals("time")) { return baseType.owner().ref(getTimeOnlyType()); - } else if (node.asText().equals("utc-millisec")) { + } else if (nodeText.equals("utc-millisec")) { return unboxIfNecessary(baseType.owner().ref(Long.class), ruleFactory.getGenerationConfig()); - } else if (node.asText().equals("regex")) { + } else if (nodeText.equals("regex")) { return baseType.owner().ref(Pattern.class); - } else if (node.asText().equals("color")) { + } else if (nodeText.equals("color")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("style")) { + } else if (nodeText.equals("style")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("phone")) { + } else if (nodeText.equals("phone")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("uri")) { + } else if (nodeText.equals("uri")) { return baseType.owner().ref(URI.class); - } else if (node.asText().equals("email")) { + } else if (nodeText.equals("email")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("ip-address")) { + } else if (nodeText.equals("ip-address")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("ipv6")) { + } else if (nodeText.equals("ipv6")) { return baseType.owner().ref(String.class); - } else if (node.asText().equals("host-name")) { + } else if (nodeText.equals("host-name")) { return baseType.owner().ref(String.class); - } - else if (node.asText().equals("uuid")) { - return baseType.owner().ref(UUID.class); + } else if (nodeText.equals("uuid")) { + return baseType.owner().ref(UUID.class); + } else if (nodeText.equals("number")) { + return getNumberType(baseType.owner()); + } else if (nodeText.equals("integer")) { + return getIntegerType(baseType.owner(), node); } else { return baseType; @@ -130,6 +137,29 @@ else if (node.asText().equals("uuid")) { } + private JType getNumberType(JCodeModel owner) { + GenerationConfig config = ruleFactory.getGenerationConfig(); + if (config.isUseBigDecimals()) { + return unboxIfNecessary(owner.ref(BigDecimal.class), config); + } else if (config.isUseDoubleNumbers()) { + return unboxIfNecessary(owner.ref(Double.class), config); + } else { + return unboxIfNecessary(owner.ref(Float.class), config); + } + } + + private JType getIntegerType(JCodeModel owner, JsonNode node) { + GenerationConfig config = ruleFactory.getGenerationConfig(); + if (config.isUseLongIntegers() || + (node.has("minimum") && node.get("minimum").isLong()) || + (node.has("maximum") && node.get("maximum").isLong())) { + return unboxIfNecessary(owner.ref(Long.class), config); + } else { + return unboxIfNecessary(owner.ref(Integer.class), config); + } + + } + private Class getDateTimeType() { String type=ruleFactory.getGenerationConfig().getDateTimeType(); if (!isEmpty(type)){ From 5ecc620d98d169f364743f7813c74cf11f863960 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Wed, 22 Jun 2016 13:22:49 -0400 Subject: [PATCH 7/7] Add autogenerated file comments --- .../jsonschema2pojo/annotations/FieldFormat.java | 16 ++++++++++++++++ .../annotations/FieldFormats.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java index edbca976c..900977bed 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormat.java @@ -1,3 +1,19 @@ +/** + * Copyright © 2010-2014 Nokia + * + * 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.jsonschema2pojo.annotations; import java.lang.annotation.ElementType; diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java index 2b36683c7..491cbcf67 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldFormats.java @@ -1,3 +1,19 @@ +/** + * Copyright © 2010-2014 Nokia + * + * 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.jsonschema2pojo.annotations; /**