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

back port annotation support for defaults and preserve-unknown-fields #129

Merged
merged 7 commits into from
Jan 9, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -84,13 +84,15 @@ public abstract class AbstractJsonSchema<T, B> {
public static final String ANNOTATION_JSON_IGNORE = "com.fasterxml.jackson.annotation.JsonIgnore";
public static final String ANNOTATION_JSON_ANY_GETTER = "com.fasterxml.jackson.annotation.JsonAnyGetter";
public static final String ANNOTATION_JSON_ANY_SETTER = "com.fasterxml.jackson.annotation.JsonAnySetter";
public static final String ANNOTATION_DEFAULT = "io.fabric8.generator.annotation.Default";
public static final String ANNOTATION_MIN = "io.fabric8.generator.annotation.Min";
public static final String ANNOTATION_MAX = "io.fabric8.generator.annotation.Max";
public static final String ANNOTATION_PATTERN = "io.fabric8.generator.annotation.Pattern";
public static final String ANNOTATION_NULLABLE = "io.fabric8.generator.annotation.Nullable";
public static final String ANNOTATION_REQUIRED = "io.fabric8.generator.annotation.Required";
public static final String ANNOTATION_NOT_NULL = "javax.validation.constraints.NotNull";
public static final String ANNOTATION_SCHEMA_FROM = "io.fabric8.crd.generator.annotation.SchemaFrom";
public static final String ANNOTATION_PERSERVE_UNKNOWN_FIELDS = "io.fabric8.crd.generator.annotation.PreserveUnknownFields";
public static final String ANNOTATION_SCHEMA_SWAP = "io.fabric8.crd.generator.annotation.SchemaSwap";

public static final String JSON_NODE_TYPE = "com.fasterxml.jackson.databind.JsonNode";
Expand Down Expand Up @@ -122,39 +124,50 @@ public static String getSchemaTypeFor(TypeRef typeRef) {
}

protected static class SchemaPropsOptions {
final Optional<Double> min;
final Optional<Double> max;
final Optional<String> pattern;
final String defaultValue;
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();
defaultValue = null;
min = null;
max = null;
pattern = null;
nullable = false;
required = false;
preserveUnknownFields = false;
}

public SchemaPropsOptions(Optional<Double> min, Optional<Double> max, Optional<String> pattern,
boolean nullable, boolean required) {
public SchemaPropsOptions(String defaultValue, Double min, Double max, String pattern,
boolean nullable, boolean required, boolean preserveUnknownFields) {
this.defaultValue = defaultValue;
this.min = min;
this.max = max;
this.pattern = pattern;
this.nullable = nullable;
this.required = required;
this.preserveUnknownFields = preserveUnknownFields;
}

public Optional<String> getDefault() {
return Optional.ofNullable(defaultValue);
}

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 All @@ -164,6 +177,10 @@ public boolean isNullable() {
public boolean getRequired() {
return nullable;
}

public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}
}

/**
Expand Down Expand Up @@ -322,11 +339,13 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna
}

SchemaPropsOptions options = new SchemaPropsOptions(
facade.defaultValue,
facade.min,
facade.max,
facade.pattern,
facade.nullable,
facade.required);
facade.required,
facade.preserveUnknownFields);

addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options);
}
Expand All @@ -351,9 +370,10 @@ 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 String defaultValue;
private Double min;
private Double max;
private String pattern;
private boolean nullable;
private boolean required;
private boolean ignored;
Expand All @@ -366,10 +386,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 @@ -383,17 +399,20 @@ static PropertyOrAccessor fromMethod(Method method, String propertyName) {
public void process() {
annotations.forEach(a -> {
switch (a.getClassRef().getFullyQualifiedName()) {
case ANNOTATION_DEFAULT:
defaultValue = (String) a.getParameters().get(VALUE);
break;
case ANNOTATION_NULLABLE:
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 @@ -420,6 +439,7 @@ public void process() {
break;
case ANNOTATION_JSON_ANY_GETTER:
case ANNOTATION_JSON_ANY_SETTER:
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS:
preserveUnknownFields = true;
break;
case ANNOTATION_SCHEMA_FROM:
Expand All @@ -437,16 +457,20 @@ public boolean isNullable() {
return nullable;
}

public Optional<String> getDefault() {
return Optional.ofNullable(defaultValue);
}

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 Down Expand Up @@ -493,9 +517,10 @@ private static class PropertyFacade {
private final Set<InternalSchemaSwap> matchedSchemaSwaps;
private String renamedTo;
private String description;
private Optional<Double> min;
private Optional<Double> max;
private Optional<String> pattern;
private String defaultValue;
private Double min;
private Double max;
private String pattern;
private boolean nullable;
private boolean required;
private boolean ignored;
Expand Down Expand Up @@ -524,9 +549,10 @@ public PropertyFacade(Property property, Map<String, Method> potentialAccessors,
if (method != null) {
propertyOrAccessors.add(PropertyOrAccessor.fromMethod(method, name));
}
min = Optional.empty();
max = Optional.empty();
pattern = Optional.empty();
defaultValue = null;
min = null;
max = null;
pattern = null;
}

public Property process() {
Expand Down Expand Up @@ -562,18 +588,10 @@ 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();
}
defaultValue = p.getDefault().orElse(defaultValue);
min = p.getMin().orElse(min);
max = p.getMax().orElse(max);
pattern = p.getPattern().orElse(pattern);

if (p.isNullable()) {
nullable = true;
Expand All @@ -585,9 +603,7 @@ public Property process() {
ignored = true;
}

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

if (p.contributeSchemaFrom()) {
schemaFrom = p.getSchemaFrom();
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class CRDGenerator {
private CRDOutput<? extends OutputStream> output;
private Map<String, CustomResourceInfo> infos;

private static final ObjectMapper YAML_MAPPER = new ObjectMapper(
public static final ObjectMapper YAML_MAPPER = new ObjectMapper(
new YAMLFactory()
.enable(Feature.MINIMIZE_QUOTES)
.enable(Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
Expand Down
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* 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 io.fabric8.crd.generator.annotation;

import java.lang.annotation.*;

/*
* Used to emit 'x-kubernetes-preserve-unknown-fields'
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PreserveUnknownFields {
}
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.crd.generator.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.crd.generator.AbstractJsonSchema;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
Expand All @@ -25,6 +26,8 @@

import java.util.List;

import static io.fabric8.crd.generator.CRDGenerator.YAML_MAPPER;

public class JsonSchema extends AbstractJsonSchema<JSONSchemaProps, JSONSchemaPropsBuilder> {

private static final JsonSchema instance = new JsonSchema();
Expand Down Expand Up @@ -58,6 +61,13 @@ public JSONSchemaPropsBuilder newBuilder() {
public void addProperty(Property property, JSONSchemaPropsBuilder builder,
JSONSchemaProps schema, SchemaPropsOptions options) {
if (schema != null) {
options.getDefault().ifPresent(s -> {
try {
schema.setDefault(YAML_MAPPER.readTree(s));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Cannot parse default value: '" + s + "' as valid YAML.");
}
});
options.getMin().ifPresent(schema::setMinimum);
options.getMax().ifPresent(schema::setMaximum);
options.getPattern().ifPresent(schema::setPattern);
Expand All @@ -66,6 +76,10 @@ public void addProperty(Property property, JSONSchemaPropsBuilder builder,
schema.setNullable(true);
}

if (options.isPreserveUnknownFields()) {
schema.setXKubernetesPreserveUnknownFields(true);
}

builder.addToProperties(property.getName(), schema);
}
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.crd.generator.v1beta1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.crd.generator.AbstractJsonSchema;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps;
Expand All @@ -25,6 +26,8 @@

import java.util.List;

import static io.fabric8.crd.generator.CRDGenerator.YAML_MAPPER;

public class JsonSchema extends AbstractJsonSchema<JSONSchemaProps, JSONSchemaPropsBuilder> {

private static final JsonSchema instance = new JsonSchema();
Expand Down Expand Up @@ -59,6 +62,13 @@ public JSONSchemaPropsBuilder newBuilder() {
public void addProperty(Property property, JSONSchemaPropsBuilder builder,
JSONSchemaProps schema, SchemaPropsOptions options) {
if (schema != null) {
options.getDefault().ifPresent(s -> {
try {
schema.setDefault(YAML_MAPPER.readTree(s));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Cannot parse default value: '" + s + "' as valid YAML.");
}
});
options.getMin().ifPresent(schema::setMinimum);
options.getMax().ifPresent(schema::setMaximum);
options.getPattern().ifPresent(schema::setPattern);
Expand All @@ -67,6 +77,10 @@ public void addProperty(Property property, JSONSchemaPropsBuilder builder,
schema.setNullable(true);
}

if (options.isPreserveUnknownFields()) {
schema.setXKubernetesPreserveUnknownFields(true);
}

builder.addToProperties(property.getName(), schema);
}
}
Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import io.fabric8.generator.annotation.Default;
import io.fabric8.generator.annotation.Max;
import io.fabric8.generator.annotation.Min;
import io.fabric8.generator.annotation.Nullable;
Expand All @@ -37,7 +38,8 @@ public class AnnotatedSpec {
private int max;
private String singleDigit;
private String nullable;
@NotNull
private String defaultValue;
@Required
private boolean emptySetter;
@Required
private boolean emptySetter2;
Expand Down Expand Up @@ -86,6 +88,11 @@ public String getNullable() {
return null;
}

@Default("my-value")
public String getDefaultValue() {
return "foo";
}

@JsonProperty
public void setEmptySetter(boolean emptySetter) {
this.emptySetter = emptySetter;
Expand Down