Skip to content

Commit

Permalink
refactor: JsonSchema remove preserveSelfUnknownFields
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Sep 19, 2022
1 parent 767355f commit 5eefe53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 59 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Fix #4383: bump snakeyaml from 1.30 to 1.31

#### New Features
* Feat: add annotation @PreserveUnknownFields for marking generated field have `x-kubernetes-preserve-unknown-fields: true` defined
* Fix #4398: add annotation @PreserveUnknownFields for marking generated field have `x-kubernetes-preserve-unknown-fields: true` defined

#### _**Note**_: Breaking changes in the API
* Fix #4350: SchemaSwap's fieldName parameter now expects a field name only, not a method or a constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ public static String getSchemaTypeFor(TypeRef typeRef) {
}

protected static class SchemaPropsOptions {
final Optional<Double> min;
final Optional<Double> max;
final Optional<String> pattern;
final Double min;
final Double max;
final String pattern;
final boolean nullable;
final boolean required;

final boolean preserveUnknownFields;

SchemaPropsOptions() {
min = Optional.empty();
max = Optional.empty();
pattern = Optional.empty();
min = null;
max = null;
pattern = null;
nullable = false;
required = false;
preserveUnknownFields = false;
}

public SchemaPropsOptions(Optional<Double> min, Optional<Double> max, Optional<String> pattern,
public SchemaPropsOptions(Double min, Double max, String pattern,
boolean nullable, boolean required, boolean preserveUnknownFields) {
this.min = min;
this.max = max;
Expand All @@ -148,15 +148,15 @@ public SchemaPropsOptions(Optional<Double> min, Optional<Double> max, Optional<S
}

public Optional<Double> getMin() {
return min;
return Optional.ofNullable(min);
}

public Optional<Double> getMax() {
return max;
return Optional.ofNullable(max);
}

public Optional<String> getPattern() {
return pattern;
return Optional.ofNullable(pattern);
}

public boolean isNullable() {
Expand Down Expand Up @@ -290,7 +290,7 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, InternalSche
facade.pattern,
facade.nullable,
facade.required,
facade.preserveSelfUnknownFields);
facade.preserveUnknownFields);

addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options);
}
Expand All @@ -313,14 +313,13 @@ private static class PropertyOrAccessor {
private final String propertyName;
private final String type;
private String renamedTo;
private Optional<Double> min;
private Optional<Double> max;
private Optional<String> pattern;
private Double min;
private Double max;
private String pattern;
private boolean nullable;
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private boolean preserveSelfUnknownFields;
private String description;
private TypeRef schemaFrom;

Expand All @@ -329,10 +328,6 @@ private PropertyOrAccessor(Collection<AnnotationRef> annotations, String name, S
this.name = name;
this.propertyName = propertyName;
type = isMethod ? "accessor" : "field";

min = Optional.empty();
max = Optional.empty();
pattern = Optional.empty();
}

static PropertyOrAccessor fromProperty(Property property) {
Expand All @@ -350,13 +345,13 @@ public void process() {
nullable = true;
break;
case ANNOTATION_MAX:
max = Optional.of((Double) a.getParameters().get(VALUE));
max = (Double) a.getParameters().get(VALUE);
break;
case ANNOTATION_MIN:
min = Optional.of((Double) a.getParameters().get(VALUE));
min = (Double) a.getParameters().get(VALUE);
break;
case ANNOTATION_PATTERN:
pattern = Optional.of((String) a.getParameters().get(VALUE));
pattern = (String) a.getParameters().get(VALUE);
break;
case ANNOTATION_NOT_NULL:
LOGGER.warn("Annotation: {} on property: {} is deprecated. Please use: {} instead", ANNOTATION_NOT_NULL, name,
Expand All @@ -383,10 +378,8 @@ public void process() {
break;
case ANNOTATION_JSON_ANY_GETTER:
case ANNOTATION_JSON_ANY_SETTER:
preserveUnknownFields = true;
break;
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS:
preserveSelfUnknownFields = true;
preserveUnknownFields = true;
break;
case ANNOTATION_SCHEMA_FROM:
schemaFrom = extractClassRef(a.getParameters().get("type"));
Expand All @@ -404,15 +397,15 @@ public boolean isNullable() {
}

public Optional<Double> getMax() {
return max;
return Optional.ofNullable(max);
}

public Optional<Double> getMin() {
return min;
return Optional.ofNullable(min);
}

public Optional<String> getPattern() {
return pattern;
return Optional.ofNullable(pattern);
}

public boolean isRequired() {
Expand All @@ -427,10 +420,6 @@ public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}

public boolean isPreserveSelfUnknownFields() {
return preserveSelfUnknownFields;
}

public String getDescription() {
return description;
}
Expand Down Expand Up @@ -461,14 +450,13 @@ private static class PropertyFacade {
private final List<PropertyOrAccessor> propertyOrAccessors = new ArrayList<>(4);
private String renamedTo;
private String description;
private Optional<Double> min;
private Optional<Double> max;
private Optional<String> pattern;
private Double min;
private Double max;
private String pattern;
private boolean nullable;
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private boolean preserveSelfUnknownFields;
private final Property original;
private String nameContributedBy;
private String descriptionContributedBy;
Expand All @@ -492,9 +480,9 @@ public PropertyFacade(Property property, Map<String, Method> potentialAccessors,
propertyOrAccessors.add(PropertyOrAccessor.fromMethod(method, name));
}
schemaFrom = schemaSwap;
min = Optional.empty();
max = Optional.empty();
pattern = Optional.empty();
min = null;
max = null;
pattern = null;
}

public Property process() {
Expand All @@ -520,18 +508,9 @@ public Property process() {
LOGGER.debug("Description for property {} has already been contributed by: {}", name, descriptionContributedBy);
}
}

if (p.getMin().isPresent()) {
min = p.getMin();
}

if (p.getMax().isPresent()) {
max = p.getMax();
}

if (p.getPattern().isPresent()) {
pattern = p.getPattern();
}
min = p.getMin().orElse(min);
max = p.getMax().orElse(max);
pattern = p.getPattern().orElse(pattern);

if (p.isNullable()) {
nullable = true;
Expand All @@ -543,13 +522,7 @@ public Property process() {
ignored = true;
}

if (p.isPreserveUnknownFields()) {
preserveUnknownFields = true;
}

if (p.isPreserveSelfUnknownFields()) {
preserveSelfUnknownFields = true;
}
preserveUnknownFields = p.isPreserveUnknownFields() || preserveUnknownFields;

if (p.contributeSchemaFrom()) {
schemaFrom = p.getSchemaFrom();
Expand Down

0 comments on commit 5eefe53

Please sign in to comment.