From 66fffa39fc72a2b6ce55ac3b67e29d7103f56347 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Sat, 21 Nov 2015 23:43:24 -0500 Subject: [PATCH 1/4] Fix node resolution for arrays before adding annotations --- .../jsonschema2pojo/annotations/PluralTitle.java | 7 +++++++ .../org/jsonschema2pojo/rules/PropertyRule.java | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java new file mode 100644 index 000000000..99efc1b43 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java @@ -0,0 +1,7 @@ +package org.jsonschema2pojo.annotations; + +/** + * Created by kat on 11/21/15. + */ +public interface PluralTitle { +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java index 6b3e0a2ba..fc039c54a 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java @@ -127,11 +127,21 @@ private void propertyAnnotations(String nodeName, JsonNode node, Schema schema, } private JsonNode resolveRefs(JsonNode node, Schema parent) { + JsonNode ref = null; if (node.has("$ref")) { - Schema refSchema = ruleFactory.getSchemaStore().create(parent, node.get("$ref").asText()); + ref = node.get("$ref"); + } else if (node.has("items")) { + JsonNode itemsNode = node.get("items"); + if (itemsNode.has("$ref")) { + ref = itemsNode.get("$ref"); + } + } + + if (ref != null) { + Schema refSchema = ruleFactory.getSchemaStore().create(parent, ref.asText()); JsonNode refNode = refSchema.getContent(); return resolveRefs(refNode, parent); - } else { + } else { return node; } } From e33eba1af8d8c2b5a423bcffb863b4eb6b487066 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Sat, 21 Nov 2015 23:43:45 -0500 Subject: [PATCH 2/4] Add custom annotation for plural title --- .../jsonschema2pojo/JsonEditorAnnotator.java | 45 ++++++++++++++++--- .../annotations/PluralTitle.java | 27 ++++++++++- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java index d9091860f..fde9ae55e 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java @@ -1,13 +1,30 @@ +/** + * 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; import java.util.Iterator; import java.util.SortedMap; import java.util.TreeMap; +import java.util.logging.Logger; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.JsonNode; -import com.sun.codemodel.JAnnotationArrayMember; -import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.*; +import org.jsonschema2pojo.annotations.PluralTitle; /** * Created by kat on 11/21/15. @@ -17,6 +34,8 @@ public class JsonEditorAnnotator extends AbstractAnnotator { // as used by json-editor static int DEFAULT_PROPERTY_ORDER = 1000; + static Logger Log = Logger.getLogger("JsonEditorAnnotator"); + @Override public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { JAnnotationArrayMember annotationValue = clazz.annotate(JsonPropertyOrder.class).paramArray("value"); @@ -24,13 +43,12 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { SortedMap fieldOrders = new TreeMap(); for (Iterator properties = propertiesNode.fieldNames(); properties.hasNext();) { - //annotationValue.param(properties.next()); String propertyName = properties.next(); JsonNode element = propertiesNode.findValue(propertyName); - JsonNode propertyOrder = element.findValue("propertyOrder"); int order = DEFAULT_PROPERTY_ORDER; - if (propertyOrder != null) { + if (element != null && element.has("propertyOrder")) { + JsonNode propertyOrder = element.findValue("propertyOrder"); order = propertyOrder.asInt(DEFAULT_PROPERTY_ORDER); } @@ -42,6 +60,23 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { } } + @Override + public void propertyField(JFieldVar field, JDefinedClass clazz, + String propertyName, JsonNode propertyNode) { + + JsonNode pluralTitleNode = propertyNode.findValue("plural_title"); + + if (pluralTitleNode != null) { + String pluralTitle = pluralTitleNode.asText(); + + if (pluralTitle != null && !pluralTitle.isEmpty()) { + JAnnotationUse generated = field.annotate(PluralTitle.class); + generated.param("value", pluralTitle); + } + } + + } + @Override public boolean isAdditionalPropertiesSupported() { return true; diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java index 99efc1b43..5ca673a5a 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java @@ -1,7 +1,32 @@ +/** + * 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; /** * Created by kat on 11/21/15. */ -public interface PluralTitle { + +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 PluralTitle { + public String value(); } From f81ebc552a4238e3e6ac30d461c057ab1c3be8c0 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Sat, 21 Nov 2015 23:50:25 -0500 Subject: [PATCH 3/4] Add autogenerated licenses --- .../main/java/org/jsonschema2pojo/JsonEditorAnnotator.java | 4 +--- .../java/org/jsonschema2pojo/annotations/PluralTitle.java | 4 ---- .../src/main/java/org/jsonschema2pojo/rules/PropertyRule.java | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java index fde9ae55e..617feced7 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java @@ -26,9 +26,7 @@ import com.sun.codemodel.*; import org.jsonschema2pojo.annotations.PluralTitle; -/** - * Created by kat on 11/21/15. - */ + public class JsonEditorAnnotator extends AbstractAnnotator { // as used by json-editor diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java index 5ca673a5a..0a13d5204 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/PluralTitle.java @@ -16,10 +16,6 @@ package org.jsonschema2pojo.annotations; -/** - * Created by kat on 11/21/15. - */ - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java index fc039c54a..481bb5412 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/PropertyRule.java @@ -141,7 +141,7 @@ private JsonNode resolveRefs(JsonNode node, Schema parent) { Schema refSchema = ruleFactory.getSchemaStore().create(parent, ref.asText()); JsonNode refNode = refSchema.getContent(); return resolveRefs(refNode, parent); - } else { + } else { return node; } } From ada0b91a4d9385f137b70bcbcdfbbea857cea9c4 Mon Sep 17 00:00:00 2001 From: Kathryn Killebrew Date: Sun, 22 Nov 2015 00:52:44 -0500 Subject: [PATCH 4/4] Add annotations for remaining needed fields --- .../jsonschema2pojo/JsonEditorAnnotator.java | 62 ++++++++++++++++--- .../annotations/Description.java | 28 +++++++++ .../annotations/FieldType.java | 28 +++++++++ .../annotations/FieldTypes.java | 24 +++++++ .../jsonschema2pojo/annotations/IsHidden.java | 28 +++++++++ .../jsonschema2pojo/annotations/Multiple.java | 28 +++++++++ .../jsonschema2pojo/annotations/Title.java | 28 +++++++++ 7 files changed, 219 insertions(+), 7 deletions(-) create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Description.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldType.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/IsHidden.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Multiple.java create mode 100644 jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Title.java diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java index 617feced7..33ca6e283 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonEditorAnnotator.java @@ -24,7 +24,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.JsonNode; import com.sun.codemodel.*; -import org.jsonschema2pojo.annotations.PluralTitle; +import org.jsonschema2pojo.annotations.*; public class JsonEditorAnnotator extends AbstractAnnotator { @@ -42,6 +42,11 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { for (Iterator properties = propertiesNode.fieldNames(); properties.hasNext();) { String propertyName = properties.next(); + + if (propertyName.equals("_localId")) { + continue; + } + JsonNode element = propertiesNode.findValue(propertyName); int order = DEFAULT_PROPERTY_ORDER; @@ -62,14 +67,57 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) { - JsonNode pluralTitleNode = propertyNode.findValue("plural_title"); + if (propertyNode.has("title")) { + String title = propertyNode.get("title").asText(); + + if (!title.isEmpty()) { + JAnnotationUse titleAnnotation = field.annotate(Title.class); + titleAnnotation.param("value", title); + } + } + + if (propertyNode.has("plural_title")) { + String pluralTitle = propertyNode.get("plural_title").asText(); + + if (!pluralTitle.isEmpty()) { + JAnnotationUse pluralTitleAnnotation = field.annotate(PluralTitle.class); + pluralTitleAnnotation.param("value", pluralTitle); + } + } + + if (propertyNode.has("multiple")) { + Boolean multiple = propertyNode.get("multiple").asBoolean(); + JAnnotationUse multipleAnnotation = field.annotate(Multiple.class); + multipleAnnotation.param("value", multiple); + } + + 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); + } else { + Log.info("No field type found for " + fieldType); + } + } + + if (propertyNode.has("options")) { + JsonNode options = propertyNode.get("options"); + if (options.has("hidden")) { + Boolean isHidden = options.get("hidden").asBoolean(); + JAnnotationUse hiddenAnnotation = field.annotate(IsHidden.class); + hiddenAnnotation.param("value", isHidden); + } + } - if (pluralTitleNode != null) { - String pluralTitle = pluralTitleNode.asText(); + if (propertyNode.has("description")) { + String description = propertyNode.get("description").asText(); - if (pluralTitle != null && !pluralTitle.isEmpty()) { - JAnnotationUse generated = field.annotate(PluralTitle.class); - generated.param("value", pluralTitle); + if (!description.isEmpty()) { + JAnnotationUse descriptionAnnotation = field.annotate(Description.class); + descriptionAnnotation.param("value", description); } } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Description.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Description.java new file mode 100644 index 000000000..b2eb84fb0 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Description.java @@ -0,0 +1,28 @@ +/** + * 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; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Description { + public String value(); +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldType.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldType.java new file mode 100644 index 000000000..ae1402bb4 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldType.java @@ -0,0 +1,28 @@ +/** + * 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; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface FieldType { + public FieldTypes value(); +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java new file mode 100644 index 000000000..e2ef0d7f5 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/FieldTypes.java @@ -0,0 +1,24 @@ +/** + * 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; + +/** + * Created by kat on 11/22/15. + */ +public enum FieldTypes { + text, image, selectlist, reference +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/IsHidden.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/IsHidden.java new file mode 100644 index 000000000..ba1f616a8 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/IsHidden.java @@ -0,0 +1,28 @@ +/** + * 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; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface IsHidden { + public boolean value(); +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Multiple.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Multiple.java new file mode 100644 index 000000000..12214fb48 --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Multiple.java @@ -0,0 +1,28 @@ +/** + * 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; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Multiple { + public boolean value(); +} diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Title.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Title.java new file mode 100644 index 000000000..95c4a37ba --- /dev/null +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/annotations/Title.java @@ -0,0 +1,28 @@ +/** + * 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; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Title { + public String value(); +}