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

feat: add annotation @PreserveUnknownFields for field #4398

Merged
merged 4 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* Fix #4383: bump snakeyaml from 1.30 to 1.31

#### New Features
* Feat: add annotation @PreserveUnknownFields for generation field
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please expand a little this sentence, for example saying that it affects the crd-generator


#### _**Note**_: Breaking changes in the API

Expand Down
Expand Up @@ -86,6 +86,7 @@ public abstract class AbstractJsonSchema<T, B> {
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 @@ -123,21 +124,25 @@ protected static class SchemaPropsOptions {
final boolean nullable;
final boolean required;

final boolean preserveUnknownFields;

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

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

public Optional<Double> getMin() {
Expand All @@ -159,6 +164,10 @@ public boolean isNullable() {
public boolean getRequired() {
return nullable;
}

public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}
}

/**
Expand Down Expand Up @@ -321,7 +330,8 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna
facade.max,
facade.pattern,
facade.nullable,
facade.required);
facade.required,
facade.preserveSelfUnknownFields);

addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options);
}
Expand Down Expand Up @@ -352,6 +362,7 @@ private static class PropertyOrAccessor {
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private boolean preserveSelfUnknownFields;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an extra field here?
Is it possible to rely on the already existing preserveUnknownFields ?

private String description;
private TypeRef schemaFrom;

Expand Down Expand Up @@ -416,6 +427,9 @@ public void process() {
case ANNOTATION_JSON_ANY_SETTER:
preserveUnknownFields = true;
break;
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS:
preserveSelfUnknownFields = true;
break;
case ANNOTATION_SCHEMA_FROM:
schemaFrom = extractClassRef(a.getParameters().get("type"));
break;
Expand Down Expand Up @@ -455,6 +469,10 @@ public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}

public boolean isPreserveSelfUnknownFields() {
return preserveSelfUnknownFields;
}

public String getDescription() {
return description;
}
Expand Down Expand Up @@ -494,6 +512,7 @@ private static class PropertyFacade {
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private boolean preserveSelfUnknownFields;
private final Property original;
private String nameContributedBy;
private String descriptionContributedBy;
Expand Down Expand Up @@ -583,6 +602,10 @@ public Property process() {
preserveUnknownFields = true;
}

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

if (p.contributeSchemaFrom()) {
schemaFrom = p.getSchemaFrom();
}
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 tweak the behavior of the crd-generator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like to emit 'x-kubernetes-preserve-unknown-fields'

*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PreserveUnknownFields {
}
Expand Up @@ -66,6 +66,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 @@ -67,6 +67,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,13 +15,15 @@
*/
package io.fabric8.crd.example.extraction;

import io.fabric8.crd.generator.annotation.PreserveUnknownFields;
import io.fabric8.crd.generator.annotation.SchemaFrom;

public class ExtractionSpec {

@SchemaFrom(type = FooExtractor.class)
private Foo foo;

@PreserveUnknownFields
private Foo bar;

}
Expand Up @@ -194,6 +194,7 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
JSONSchemaProps bar = spec.get("bar");
Map<String, JSONSchemaProps> barProps = bar.getProperties();
assertNotNull(barProps);
assertTrue(bar.getXKubernetesPreserveUnknownFields());

// you can change everything
assertEquals("integer", barProps.get("BAZ").getType());
Expand Down
22 changes: 22 additions & 0 deletions doc/CRD-generator.md
Expand Up @@ -357,6 +357,28 @@ Corresponding `x-kubernetes-preserve-unknown-fields: true` will be generated in
x-kubernetes-preserve-unknown-fields: true
```

You can also annotation a field with @PreserveUnknownFields:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you, please, add the annotation to the cheatsheet below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also s/annotation/annotate/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also s/annotation/annotate/ ?

sorry I can't find where it is, can I ask for help😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace annotation with annotate so that the sentence reads:

You can also annotate a field with @PreserveUnknownFields:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace annotation with annotate so that the sentence reads:

sorry😂……fixed


```java
interface ExampleInterface {}

public class ExampleSpec {
@PreserveUnknownFields
ExampleInterface someValue;
}
```

will be generated as:

```yaml
spec:
properties:
someValue:
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
```

## Features cheatsheet

| Annotation | Description |
Expand Down