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

Use additionalProperties instead of x-kubernetes-preserve-unknown-fields for Map<String, String> fields #9508

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
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,12 @@ private ObjectNode buildArraySchema(ApiVersion crApiVersion, Property property,
|| long.class.equals(elementType)) {
itemResult.put("type", "integer");
} else if (Map.class.equals(elementType)) {
preserveUnknownFields(itemResult);
if (isStringStringMap(propertyType)) {
preserveUnknownStringFields(itemResult);
} else {
preserveUnknownFields(itemResult);
}

itemResult.put("type", "object");
} else if (elementType.isEnum()) {
itemResult.put("type", "string");
Expand All @@ -815,15 +820,24 @@ private ObjectNode buildArraySchema(ApiVersion crApiVersion, Property property,
return result;
}

private ObjectNode buildBasicTypeSchema(AnnotatedElement element, Class type) {
private boolean isStringStringMap(PropertyType propertyType) {
java.lang.reflect.Type[] types = ((ParameterizedType) propertyType.getGenericType()).getActualTypeArguments();
return String.class.equals(types[0]) && String.class.equals(types[1]);
}

private ObjectNode buildBasicTypeSchema(Property element, Class type) {
ObjectNode result = nf.objectNode();

String typeName;
Type typeAnno = element.getAnnotation(Type.class);
if (typeAnno == null) {
typeName = typeName(type);
if (Map.class.equals(type)) {
preserveUnknownFields(result);
if (isStringStringMap(element.getType())) {
preserveUnknownStringFields(result);
} else {
preserveUnknownFields(result);
}
}
} else {
typeName = typeAnno.value();
Expand All @@ -839,6 +853,13 @@ private void preserveUnknownFields(ObjectNode result) {
}
}

private void preserveUnknownStringFields(ObjectNode result) {
if (crdApiVersion.compareTo(V1) >= 0) {
ObjectNode additionalProperties = result.putObject("additionalProperties");
additionalProperties.put("type", "string");
}
}

private void addDescription(ApiVersion crApiVersion, ObjectNode result, AnnotatedElement element) {
Description description = selectVersion(crApiVersion, element, Description.class);
if (description != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
)
)
@OneOf({@OneOf.Alternative(@OneOf.Alternative.Property("either")), @OneOf.Alternative(@OneOf.Alternative.Property("or"))})
@OneOf({@OneOf.Alternative(@OneOf.Alternative.Property("either")), @OneOf.Alternative(@OneOf.Alternative.Property("or")), @OneOf.Alternative({@OneOf.Alternative.Property("mapStringString"), @OneOf.Alternative.Property("mapStringObject")})})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ExampleCrd<T, U extends Number, V extends U> extends CustomResource {

Expand Down Expand Up @@ -152,7 +152,9 @@ public void setBar(String bar) {
}
}

private Map<String, Object> mapProperty;
private Map<String, Object> mapStringObject;

private Map<String, String> mapStringString;

private PolymorphicTop polymorphicProperty;

Expand Down Expand Up @@ -278,12 +280,20 @@ public void setObjectProperty(ObjectProperty objectProperty) {
this.objectProperty = objectProperty;
}

public Map<String, Object> getMapProperty() {
return mapProperty;
public Map<String, Object> getMapStringObject() {
return mapStringObject;
}

public void setMapStringObject(Map<String, Object> mapStringObject) {
this.mapStringObject = mapStringObject;
}

public Map<String, String> getMapStringString() {
return mapStringString;
}

public void setMapProperty(Map<String, Object> mapProperty) {
this.mapProperty = mapProperty;
public void setMapStringString(Map<String, String> mapStringString) {
this.mapStringString = mapStringString;
}

public PolymorphicTop getPolymorphicProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
|xref:type-Number-{context}[`Number`] array of dimension 2
|longProperty 1.2+<.<a|An example long property.
|integer
|mapProperty 1.2+<.<a|
|mapStringObject 1.2+<.<a|
|map
|mapStringString 1.2+<.<a|
|map
|normalEnum 1.2+<.<a|
|string (one of [BAR, FOO])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -158,7 +159,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -190,7 +192,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -209,7 +212,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -245,7 +249,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -264,7 +269,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -296,7 +302,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -315,7 +322,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -491,9 +499,13 @@ spec:
example: 42
minimum: 42
description: An example long property.
mapProperty:
mapStringObject:
x-kubernetes-preserve-unknown-fields: true
type: object
mapStringString:
additionalProperties:
type: string
type: object
normalEnum:
type: string
enum:
Expand Down Expand Up @@ -549,6 +561,12 @@ spec:
or: {}
required:
- or
- properties:
mapStringString: {}
mapStringObject: {}
required:
- mapStringString
- mapStringObject
required:
- stringProperty
- name: v1beta1
Expand Down Expand Up @@ -675,7 +693,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -694,7 +713,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -726,7 +746,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -745,7 +766,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -781,7 +803,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -800,7 +823,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -832,7 +856,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaceSelector:
type: object
Expand All @@ -851,7 +876,8 @@ spec:
items:
type: string
matchLabels:
x-kubernetes-preserve-unknown-fields: true
additionalProperties:
type: string
type: object
namespaces:
type: array
Expand Down Expand Up @@ -1027,9 +1053,13 @@ spec:
example: 42
minimum: 42
description: An example long property.
mapProperty:
mapStringObject:
x-kubernetes-preserve-unknown-fields: true
type: object
mapStringString:
additionalProperties:
type: string
type: object
normalEnum:
type: string
enum:
Expand Down Expand Up @@ -1085,5 +1115,11 @@ spec:
or: {}
required:
- or
- properties:
mapStringString: {}
mapStringObject: {}
required:
- mapStringString
- mapStringObject
required:
- stringProperty
Loading
Loading