Skip to content

Commit

Permalink
Merge pull request #3258 from Hoovs/Issue-3257
Browse files Browse the repository at this point in the history
Issue #3257 add json ignored required field
  • Loading branch information
frantuma authored Dec 5, 2020
2 parents c3d3509 + a00f0a7 commit 83c4361
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,9 @@ public Object clone() {
return cloned;
}

@JsonIgnore
public List<String> getSpecSpecifiedRequired() {
return required;
}

}
295 changes: 157 additions & 138 deletions modules/swagger-models/src/test/java/io/swagger/models/ModelImplTest.java
Original file line number Diff line number Diff line change
@@ -1,138 +1,157 @@
package io.swagger.models;

import io.swagger.TestUtils;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.Property;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

@PrepareForTest({})
public class ModelImplTest extends PowerMockTestCase {

private ModelImpl instance;

Object[] propertiesAndValues;

@BeforeMethod
public void setUp() throws Exception {
instance = new ModelImpl();
propertiesAndValues = new Object[]{"additionalProperties", new ArrayProperty(), "description", "description",
"discriminator", "discriminator", "example", new Object(), "format", "format", "isSimple", true, "name",
"name", "properties", new HashMap<String, Property>(), "required", new ArrayList<String>(), "type",
"type", "xml", new Xml(), "defaultValue", "defaultValue",};
}

@Test
public void testClone() {
// given
propertiesAndValues = new Object[]{"additionalProperties", new ArrayProperty(), "description", "description",
"discriminator", "discriminator", "example", new Object(), "isSimple", true, "name", "name",
"properties", new HashMap<String, Property>(), "required", new ArrayList<String>(), "type", "type",
"xml", new Xml(), "defaultValue", "defaultValue",};
TestUtils.testClone(instance, propertiesAndValues);
}

@Test
public void testGetProperties() {
// then
assertNull(instance.getProperties(), "New instance must have null as properties");
}

@Test
public void testEnum() {
// given
List<String> _enum = new ArrayList<String>();
assertEquals(instance._enum(_enum).getEnum(), _enum);
instance.setEnum(null);
String value = "value";

// when
instance._enum(value);

// then
assertTrue(instance.getEnum().contains(value), "The enums list must contain the new one");
}

@Test
public void testConstructor() {
// when
instance = new ModelImpl();

// then
assertNull(instance.getDiscriminator(), "New instance must have null discriminator");
assertNull(instance.getDescription(), "New instance must have null description");
assertFalse(instance.isSimple(), "New instance must not be simple");
assertNull(instance.getAdditionalProperties(), "New instance must have null additionalProperties");
assertNull(instance.getExample(), "New instance must have null example");
assertNull(instance.getDefaultValue(), "New instance must have null default value");
assertNull(instance.getXml(), "New instance must have null Xml");
}

@Test
public void testProperty() {
// given
String key = "key";
Property property = new ArrayProperty();

// when
instance.property(key, property);

// then
assertEquals(instance.getProperties().get(key), property,
"Must be able to retrieve the set value from the map");

assertTrue(instance.required(key).getRequired().contains(key),
"The set key must be contained in the required list");
}

@Test
public void testSetRequired() {
// given
String required = "required";
Property property = new ArrayProperty();
instance.property(required, property);

// when
instance.setRequired(Arrays.asList(required));

// then
assertTrue(instance.getRequired().contains(required), "The set key must be contained in the required list");

}

@Test
public void testAddProperty() {

// given
String badKey = "badKey";
String key = "key";
Property property = new ArrayProperty();
instance.property(key, property);

// when
instance.addProperty(badKey, null);

// then
assertNull(instance.getProperties().get(badKey), "The bad key must not be added to the properties");

// given
instance.setRequired(Arrays.asList(key));

// when
instance.addProperty(key, property);
assertEquals(instance.getProperties().get(key), property,
"Must be able to retrieve the set value from the map");
}
}
package io.swagger.models;

import io.swagger.TestUtils;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.Property;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

@PrepareForTest({})
public class ModelImplTest extends PowerMockTestCase {

private ModelImpl instance;

Object[] propertiesAndValues;

@BeforeMethod
public void setUp() throws Exception {
instance = new ModelImpl();
propertiesAndValues = new Object[]{"additionalProperties", new ArrayProperty(), "description", "description",
"discriminator", "discriminator", "example", new Object(), "format", "format", "isSimple", true, "name",
"name", "properties", new HashMap<String, Property>(), "required", new ArrayList<String>(), "type",
"type", "xml", new Xml(), "defaultValue", "defaultValue",};
}

@Test
public void testClone() {
// given
propertiesAndValues = new Object[]{"additionalProperties", new ArrayProperty(), "description", "description",
"discriminator", "discriminator", "example", new Object(), "isSimple", true, "name", "name",
"properties", new HashMap<String, Property>(), "required", new ArrayList<String>(), "type", "type",
"xml", new Xml(), "defaultValue", "defaultValue",};
TestUtils.testClone(instance, propertiesAndValues);
}

@Test
public void testGetProperties() {
// then
assertNull(instance.getProperties(), "New instance must have null as properties");
}

@Test
public void testEnum() {
// given
List<String> _enum = new ArrayList<String>();
assertEquals(instance._enum(_enum).getEnum(), _enum);
instance.setEnum(null);
String value = "value";

// when
instance._enum(value);

// then
assertTrue(instance.getEnum().contains(value), "The enums list must contain the new one");
}

@Test
public void testConstructor() {
// when
instance = new ModelImpl();

// then
assertNull(instance.getDiscriminator(), "New instance must have null discriminator");
assertNull(instance.getDescription(), "New instance must have null description");
assertFalse(instance.isSimple(), "New instance must not be simple");
assertNull(instance.getAdditionalProperties(), "New instance must have null additionalProperties");
assertNull(instance.getExample(), "New instance must have null example");
assertNull(instance.getDefaultValue(), "New instance must have null default value");
assertNull(instance.getXml(), "New instance must have null Xml");
}

@Test
public void testProperty() {
// given
String key = "key";
Property property = new ArrayProperty();

// when
instance.property(key, property);

// then
assertEquals(instance.getProperties().get(key), property,
"Must be able to retrieve the set value from the map");

assertTrue(instance.required(key).getRequired().contains(key),
"The set key must be contained in the required list");
}

@Test
public void testSetRequired() {
// given
String required = "required";
Property property = new ArrayProperty();
instance.property(required, property);

// when
instance.setRequired(Arrays.asList(required));

// then
assertTrue(instance.getRequired().contains(required), "The set key must be contained in the required list");

}

@Test
public void testAddProperty() {

// given
String badKey = "badKey";
String key = "key";
Property property = new ArrayProperty();
instance.property(key, property);

// when
instance.addProperty(badKey, null);

// then
assertNull(instance.getProperties().get(badKey), "The bad key must not be added to the properties");

// given
instance.setRequired(Arrays.asList(key));

// when
instance.addProperty(key, property);
assertEquals(instance.getProperties().get(key), property,
"Must be able to retrieve the set value from the map");
}

@Test
public void testAddRequiredOnNonExistingProperty() {
// given
String badPropertyName = "missingProperty";
String key = "key";
Property property = new ArrayProperty();
instance.property(key, property);
instance.addRequired(key);
assertEquals(1, instance.getRequired().size());
assertEquals(1, instance.getSpecSpecifiedRequired().size());

// when
instance.addRequired(badPropertyName);

// then
assertEquals(1, instance.getRequired().size());
assertEquals(2, instance.getSpecSpecifiedRequired().size());
}
}

0 comments on commit 83c4361

Please sign in to comment.