-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Check schema required array before is required flag #964
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
Merged
joelittlejohn
merged 3 commits into
joelittlejohn:master
from
newmen:fix-required-or-optional
Mar 27, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
131 changes: 131 additions & 0 deletions
131
jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/PropertyRuleTest.java
This file contains hidden or 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,131 @@ | ||
/** | ||
* Copyright © 2010-2017 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 static org.hamcrest.MatcherAssert.*; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.BooleanNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.sun.codemodel.JClassAlreadyExistsException; | ||
import com.sun.codemodel.JCodeModel; | ||
import com.sun.codemodel.JDefinedClass; | ||
import com.sun.codemodel.JType; | ||
import org.jsonschema2pojo.GenerationConfig; | ||
import org.jsonschema2pojo.NoopAnnotator; | ||
import org.jsonschema2pojo.Schema; | ||
import org.jsonschema2pojo.SchemaStore; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class PropertyRuleTest { | ||
private static final String TARGET_CLASS_NAME = PropertyRuleTest.class.getName() + ".DummyClass"; | ||
|
||
private static final String internalFieldName = "internalRequired"; | ||
private static final String targetFieldName = "requiredFoo"; | ||
|
||
private final GenerationConfig config = mock(GenerationConfig.class); | ||
private final PropertyRule rule = new PropertyRule(new RuleFactory(config, new NoopAnnotator(), new SchemaStore())); | ||
|
||
private ObjectMapper mapper; | ||
|
||
@Before | ||
public void setup() { | ||
mapper = new ObjectMapper(); | ||
|
||
when(config.isIncludeGetters()).thenReturn(true); | ||
when(config.isUseOptionalForGetters()).thenReturn(true); | ||
} | ||
|
||
private String getGeneratedMethodTypeName(JDefinedClass jclass) { | ||
return jclass.getMethod("getRequiredFoo", new JType[] {}).type().name(); | ||
} | ||
|
||
private Schema getMockedSchema(ObjectNode parentNode) { | ||
Schema schema = mock(Schema.class); | ||
when(schema.getContent()).thenReturn(parentNode); | ||
when(schema.deriveChildSchema(any())).thenReturn(schema); | ||
return schema; | ||
} | ||
|
||
private JDefinedClass applyRule(ObjectNode propertyNode, ObjectNode parentNode) throws JClassAlreadyExistsException { | ||
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME); | ||
return rule.apply(targetFieldName, propertyNode, parentNode, jclass, getMockedSchema(parentNode)); | ||
} | ||
|
||
@Test | ||
public void applyRequiredByTopArray() throws JClassAlreadyExistsException { | ||
ObjectNode propertyNode = mapper.createObjectNode(); | ||
propertyNode.set("required", mapper.createArrayNode().add(internalFieldName)); | ||
propertyNode.set("properties", mapper.createObjectNode().set(internalFieldName, mapper.createObjectNode())); | ||
|
||
ObjectNode parentNode = mapper.createObjectNode(); | ||
parentNode.set("required", mapper.createArrayNode().add(targetFieldName)); | ||
parentNode.set("properties", mapper.createObjectNode().set(targetFieldName, propertyNode)); | ||
|
||
JDefinedClass jclass = applyRule(propertyNode, parentNode); | ||
|
||
assertThat(jclass, notNullValue()); | ||
assertThat(getGeneratedMethodTypeName(jclass), is("RequiredFoo")); | ||
} | ||
|
||
@Test | ||
public void applyNotRequiredByTopArray() throws JClassAlreadyExistsException { | ||
ObjectNode propertyNode = mapper.createObjectNode(); | ||
propertyNode.set("required", mapper.createArrayNode().add(internalFieldName)); | ||
propertyNode.set("properties", mapper.createObjectNode().set(internalFieldName, mapper.createObjectNode())); | ||
|
||
ObjectNode parentNode = mapper.createObjectNode(); | ||
parentNode.set("properties", mapper.createObjectNode().set(targetFieldName, propertyNode)); | ||
|
||
JDefinedClass jclass = applyRule(propertyNode, parentNode); | ||
|
||
assertThat(jclass, notNullValue()); | ||
assertThat(getGeneratedMethodTypeName(jclass), is("Optional<RequiredFoo>")); | ||
} | ||
|
||
@Test | ||
public void applyRequiredByFlag() throws JClassAlreadyExistsException { | ||
ObjectNode propertyNode = mapper.createObjectNode(); | ||
propertyNode.set("required", BooleanNode.TRUE); | ||
propertyNode.set("properties", mapper.createObjectNode().set(internalFieldName, mapper.createObjectNode())); | ||
|
||
ObjectNode parentNode = mapper.createObjectNode(); | ||
parentNode.set("properties", mapper.createObjectNode().set(targetFieldName, propertyNode)); | ||
|
||
JDefinedClass jclass = applyRule(propertyNode, parentNode); | ||
|
||
assertThat(jclass, notNullValue()); | ||
assertThat(getGeneratedMethodTypeName(jclass), is("RequiredFoo")); | ||
} | ||
|
||
@Test | ||
public void applyNotRequiredByFlag() throws JClassAlreadyExistsException { | ||
ObjectNode propertyNode = mapper.createObjectNode(); | ||
propertyNode.set("properties", mapper.createObjectNode().set(internalFieldName, mapper.createObjectNode())); | ||
|
||
ObjectNode parentNode = mapper.createObjectNode(); | ||
parentNode.set("properties", mapper.createObjectNode().set(targetFieldName, propertyNode)); | ||
|
||
JDefinedClass jclass = applyRule(propertyNode, parentNode); | ||
|
||
assertThat(jclass, notNullValue()); | ||
assertThat(getGeneratedMethodTypeName(jclass), is("Optional<RequiredFoo>")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can change the checking order here if you feel the increasing performance here.
We can do that because
hasFlag
is returning boolean independently and if it returnsfalse
then we check the toprequired
array. In the previous implementation, we did not check the toprequired
array if the test of flag returned `false.