Skip to content
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

Remove required entries when property is removed #452

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Smithy Changelog

## 1.0.4 (TBD)

### Features

* Ensure that when a property is removed from a JSON schema object, that a corresponding "required"
entry is also removed.

## 1.0.3 (2020-05-26)

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.jsonschema;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -562,7 +563,7 @@ public static final class Builder implements SmithyBuilder<Schema> {

private Integer maxProperties;
private Integer minProperties;
private Collection<String> required = ListUtils.of();
private Collection<String> required = new ArrayList<>();
private Map<String, Schema> properties = new HashMap<>();
private Schema additionalProperties;
private Schema propertyNames;
Expand Down Expand Up @@ -688,7 +689,11 @@ public Builder minProperties(Integer minProperties) {
}

public Builder required(Collection<String> required) {
this.required = required == null ? ListUtils.of() : required;
if (required == null) {
this.required.clear();
} else {
this.required = new ArrayList<>(required);
}
return this;
}

Expand All @@ -709,6 +714,7 @@ public Builder putProperty(String key, Schema value) {

public Builder removeProperty(String key) {
properties.remove(key);
required.remove(key);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.smithy.jsonschema;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

Expand All @@ -24,6 +25,7 @@
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SetUtils;

public class SchemaTest {
Expand Down Expand Up @@ -180,4 +182,19 @@ public void ignoresNegativeNumericPositions() {

assertThat(schema.selectSchema("allOf", "-1"), equalTo(Optional.empty()));
}

@Test
public void removingPropertiesRemovesRequiredPropertiesToo() {
Schema schema = Schema.builder()
.removeProperty("notThere")
.required(null)
.putProperty("foo", Schema.builder().build())
.putProperty("bar", Schema.builder().build())
.required(ListUtils.of("foo", "bar"))
.removeProperty("foo")
.build();

assertThat(schema.getProperties().keySet(), contains("bar"));
assertThat(schema.getRequired(), contains("bar"));
}
}