Skip to content

Commit

Permalink
Merge pull request #78 from Emilio-Pega/oas3-null-condition-name
Browse files Browse the repository at this point in the history
Check to make sure property schema name is non-null
  • Loading branch information
viclovsky authored Apr 26, 2021
2 parents 518cec8 + 37b64b7 commit a900722
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -25,11 +26,12 @@ public boolean check(Operation operation) {
}
Optional<Schema> schema = operation.getRequestBody().getContent().entrySet()
.stream()
.filter(o -> o.getKey().equals(mediaTypeName))
.filter(o -> mediaTypeName.equals(o.getKey()))
.map(o -> o.getValue().getSchema())
.filter(Objects::nonNull)
.flatMap(o -> (Stream<Schema>) o.getProperties().values().stream())
.filter(o -> o.getName().equals(propertyName))
.flatMap(o -> (Stream<Map.Entry<String, Schema>>) o.getProperties().entrySet().stream())
.filter(o -> propertyName.equals(o.getKey()))
.map(o -> o.getValue())
.findFirst();
return check(schema);
}
Expand All @@ -38,7 +40,9 @@ public String getPropertyName() {
return propertyName;
}

public String getMediaTypeName() { return mediaTypeName; }
public String getMediaTypeName() {
return mediaTypeName;
}

public PropertyConditionPredicate setPropertyName(String propertyName) {
this.propertyName = propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -28,15 +30,15 @@ public List<Condition> createCondition(Operation operation) {

private Stream<Condition> processMediaType(String mediaTypeName, MediaType mediaType) {
if (mediaType.getSchema() != null && mediaType.getSchema().getProperties() != null) {
return ((Collection<Schema>) mediaType.getSchema().getProperties().values())
return ((Set<Map.Entry<String, Schema>>) mediaType.getSchema().getProperties().entrySet())
.stream()
.map(s -> processProperty(mediaTypeName, s))
.map(s -> processProperty(mediaTypeName, s.getKey(), s.getValue()))
.filter(Objects::nonNull);
} else {
return null;
}
}

protected abstract Condition processProperty(String mediaTypeName, Schema schema);
protected abstract Condition processProperty(String mediaTypeName, String name, Schema schema);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
public class PropertyEnumAllValuesRule extends PropertyConditionRule {

@Override
protected Condition processProperty(String mediaTypeName, Schema schema) {
protected Condition processProperty(String mediaTypeName, String name, Schema schema) {
List<String> enums = SwaggerSpecificationProcessor.extractEnum(schema);
if (mediaTypeName != null && enums != null && !enums.isEmpty()) {
if (schema != null && name != null && mediaTypeName != null
&& enums != null && !enums.isEmpty()) {
return new SinglePredicateCondition(
String.format("«%s» contains all values from enum %s", schema.getName(), enums),
String.format("«%s» contains all values from enum %s", name, enums),
"",
new PropertyValueConditionPredicate(mediaTypeName, schema.getName(), enums)
new PropertyValueConditionPredicate(mediaTypeName, name, enums)
);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
public class PropertyNotEmptyRule extends PropertyConditionRule {

@Override
protected Condition processProperty(String mediaTypeName, Schema schema) {
if (schema != null && mediaTypeName != null) {
protected Condition processProperty(String mediaTypeName, String name, Schema schema) {
if (schema != null && name != null && mediaTypeName != null) {
return new SinglePredicateCondition(
String.format("«%s» is not empty", schema.getName()),
String.format("«%s» is not empty", name),
"",
new DefaultPropertyConditionPredicate(mediaTypeName, schema.getName(), false)
new DefaultPropertyConditionPredicate(mediaTypeName, name, false)
);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
public class PropertyNotOnlyEnumValuesRule extends PropertyConditionRule {

@Override
protected Condition processProperty(String mediaTypeName, Schema schema) {
protected Condition processProperty(String mediaTypeName, String name, Schema schema) {
List<String> enums = SwaggerSpecificationProcessor.extractEnum(schema);
if (enums != null && !enums.isEmpty()) {
if (schema != null && name != null && mediaTypeName != null
&& enums != null && !enums.isEmpty()) {
return new SinglePredicateCondition(
String.format("«%s» contains all values from enum %s", schema.getName(), enums),
String.format("«%s» contains all values from enum %s", name, enums),
"",
new PropertyValueNotOnlyConditionPredicate(mediaTypeName, schema.getName(), enums)
new PropertyValueNotOnlyConditionPredicate(mediaTypeName, name, enums)
);
}
return null;
Expand Down

0 comments on commit a900722

Please sign in to comment.