-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from dmcfarland/master
Adding support for required array from http://tools.ietf.org/html/draft-... Closes #323
- Loading branch information
Showing
9 changed files
with
367 additions
and
21 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
100 changes: 100 additions & 0 deletions
100
jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/RequiredArrayRule.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,100 @@ | ||
/** | ||
* 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.rules; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.sun.codemodel.*; | ||
import org.jsonschema2pojo.Schema; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static org.apache.commons.lang3.StringUtils.capitalize; | ||
|
||
/** | ||
* Applies the "required" JSON schema rule. | ||
* | ||
* @see <a | ||
* href="http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.3">http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.3</a> | ||
*/ | ||
public class RequiredArrayRule implements Rule<JDefinedClass, JDefinedClass> { | ||
|
||
private final RuleFactory ruleFactory; | ||
|
||
public static final String REQUIRED_COMMENT_TEXT = "\n(Required)"; | ||
|
||
protected RequiredArrayRule(RuleFactory ruleFactory) { | ||
this.ruleFactory = ruleFactory; | ||
} | ||
|
||
@Override | ||
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) { | ||
List<String> requiredFieldMethods = new ArrayList<String>(); | ||
|
||
for (Iterator iterator = node.elements(); iterator.hasNext(); ) { | ||
String fieldName = ((JsonNode) iterator.next()).asText(); | ||
JFieldVar field = jclass.fields().get(fieldName); | ||
|
||
if (field == null) { | ||
continue; | ||
} | ||
|
||
addJavaDoc(field); | ||
|
||
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) { | ||
addNotNullAnnotation(field); | ||
} | ||
|
||
requiredFieldMethods.add(getGetterName(fieldName, field.type())); | ||
requiredFieldMethods.add(getSetterName(fieldName)); | ||
} | ||
|
||
updateGetterSetterJavaDoc(jclass, requiredFieldMethods); | ||
|
||
return jclass; | ||
} | ||
|
||
private void updateGetterSetterJavaDoc(JDefinedClass jclass, List<String> requiredFieldMethods) { | ||
for (Iterator methods = jclass.methods().iterator(); methods.hasNext(); ) { | ||
JMethod method = (JMethod) methods.next(); | ||
if (requiredFieldMethods.contains(method.name())) { | ||
addJavaDoc(method); | ||
} | ||
} | ||
} | ||
|
||
private void addNotNullAnnotation(JFieldVar field) { | ||
field.annotate(NotNull.class); | ||
} | ||
|
||
|
||
private void addJavaDoc(JDocCommentable docCommentable) { | ||
JDocComment javadoc = docCommentable.javadoc(); | ||
javadoc.append(REQUIRED_COMMENT_TEXT); | ||
} | ||
|
||
private String getSetterName(String propertyName) { | ||
return ruleFactory.getNameHelper().getSetterName(propertyName); | ||
} | ||
|
||
private String getGetterName(String propertyName, JType type) { | ||
return ruleFactory.getNameHelper().getGetterName(propertyName, type); | ||
} | ||
|
||
} |
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
92 changes: 92 additions & 0 deletions
92
jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/RequiredArrayRuleTest.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,92 @@ | ||
/** | ||
* 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.rules; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.sun.codemodel.*; | ||
import org.jsonschema2pojo.GenerationConfig; | ||
import org.junit.Test; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.util.Collection; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class RequiredArrayRuleTest { | ||
|
||
private static final String TARGET_CLASS_NAME = RequiredArrayRuleTest.class.getName() + ".DummyClass"; | ||
|
||
private RequiredArrayRule rule = new RequiredArrayRule(new RuleFactory()); | ||
|
||
@Test | ||
public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException { | ||
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); | ||
|
||
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar"); | ||
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
ArrayNode requiredNode = mapper.createArrayNode().add("fooBar"); | ||
|
||
rule.apply("Class", requiredNode, jclass, null); | ||
|
||
JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc(); | ||
JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc(); | ||
|
||
|
||
assertThat(fooBarJavaDoc.size(), is(1)); | ||
assertThat((String) fooBarJavaDoc.get(0), is(RequiredRule.REQUIRED_COMMENT_TEXT)); | ||
|
||
assertThat(fooJavaDoc.size(), is(0)); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldUpdateAnnotations() throws JClassAlreadyExistsException { | ||
setupRuleFactoryToIncludeJsr303(); | ||
|
||
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); | ||
|
||
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar"); | ||
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
ArrayNode requiredNode = mapper.createArrayNode().add("fooBar"); | ||
|
||
rule.apply("Class", requiredNode, jclass, null); | ||
|
||
Collection<JAnnotationUse> fooBarAnnotations = jclass.fields().get("fooBar").annotations(); | ||
Collection<JAnnotationUse> fooAnnotations = jclass.fields().get("foo").annotations(); | ||
|
||
assertThat(fooBarAnnotations.size(), is(1)); | ||
assertThat(fooBarAnnotations.iterator().next().getAnnotationClass().name(), is(NotNull.class.getSimpleName())); | ||
|
||
assertThat(fooAnnotations.size(), is(0)); | ||
} | ||
|
||
private void setupRuleFactoryToIncludeJsr303() { | ||
GenerationConfig config = mock(GenerationConfig.class); | ||
RuleFactory ruleFactory = new RuleFactory(); | ||
ruleFactory.setGenerationConfig(config); | ||
rule = new RequiredArrayRule(ruleFactory); | ||
when(config.isIncludeJsr303Annotations()).thenReturn(true); | ||
} | ||
} |
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
Oops, something went wrong.