Skip to content

Commit

Permalink
replacing the YAML_MAPPER with standard serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Apr 29, 2024
1 parent f239671 commit f1b5490
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.BeanProperty;
Expand Down Expand Up @@ -79,7 +78,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.fabric8.crdv2.generator.CRDGenerator.YAML_MAPPER;
import static java.util.Optional.ofNullable;

/**
Expand Down Expand Up @@ -252,9 +250,9 @@ public void updateSchema(T schema) {

if (defaultValue != null) {
try {
schema.setDefault(YAML_MAPPER.readTree(defaultValue));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Cannot parse default value: '" + defaultValue + "' as valid YAML.");
schema.setDefault(resolvingContext.kubernetesSerialization.convertValue(defaultValue, JsonNode.class));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Cannot parse default value: '" + defaultValue + "' as valid YAML.", e);
}
}
if (nullable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.fabric8.crdv2.generator.v1.CustomResourceHandler;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,6 +35,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -58,6 +61,7 @@ public class CRDGenerator {
private boolean parallel;
private boolean implicitPreserveUnknownFields;
private ObjectMapper objectMapper;
private KubernetesSerialization kubernetesSerialization;
private Map<String, CustomResourceInfo> infos;

// TODO: why not rely on the standard fabric8 yaml mapping
Expand Down Expand Up @@ -92,8 +96,9 @@ public CRDGenerator withParallelGenerationEnabled(boolean parallel) {
return this;
}

public CRDGenerator withObjectMapper(ObjectMapper mapper) {
public CRDGenerator withObjectMapper(ObjectMapper mapper, KubernetesSerialization kubernetesSerialization) {
this.objectMapper = mapper;
this.kubernetesSerialization = kubernetesSerialization;
return this;
}

Expand Down Expand Up @@ -173,7 +178,7 @@ public CRDGenerationInfo detailedGenerate() {
if (this.objectMapper == null) {
context = ResolvingContext.defaultResolvingContext(implicitPreserveUnknownFields);
} else {
context = new ResolvingContext(this.objectMapper, implicitPreserveUnknownFields);
context = new ResolvingContext(this.objectMapper, this.kubernetesSerialization, implicitPreserveUnknownFields);
}

for (CustomResourceInfo info : infos.values()) {
Expand Down Expand Up @@ -208,7 +213,8 @@ public void emitCrd(HasMetadata crd, CRDGenerationInfo crdGenerationInfo) {
outputStream.write(
"# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!\n"
.getBytes());
YAML_MAPPER.writeValue(outputStream, crd);
String yaml = Serialization.asYaml(crd);
outputStream.write(yaml.getBytes(StandardCharsets.UTF_8));
final URI fileURI = output.crdURI(outputName);
crdGenerationInfo.add(crdName, version, fileURI);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public JsonObjectFormatVisitor expectObjectFormat(JavaType convertedType) {

final JsonSchemaGenerator generator;
final ObjectMapper objectMapper;
final KubernetesSerialization kubernetesSerialization;
final Map<String, GeneratorObjectSchema> uriToJacksonSchema;
final boolean implicitPreserveUnknownFields;

Expand All @@ -105,21 +106,22 @@ public static ResolvingContext defaultResolvingContext(boolean implicitPreserveU
if (DEFAULT_KUBERNETES_SERIALIZATION == null) {
DEFAULT_KUBERNETES_SERIALIZATION = new AccessibleKubernetesSerialization();
}
return new ResolvingContext(DEFAULT_KUBERNETES_SERIALIZATION.getMapper(), implicitPreserveUnknownFields);
return new ResolvingContext(DEFAULT_KUBERNETES_SERIALIZATION.getMapper(), DEFAULT_KUBERNETES_SERIALIZATION, implicitPreserveUnknownFields);
}

public ResolvingContext forkContext() {
return new ResolvingContext(objectMapper, uriToJacksonSchema, implicitPreserveUnknownFields);
return new ResolvingContext(objectMapper, kubernetesSerialization, uriToJacksonSchema, implicitPreserveUnknownFields);
}

public ResolvingContext(ObjectMapper mapper, boolean implicitPreserveUnknownFields) {
this(mapper, new ConcurrentHashMap<>(), implicitPreserveUnknownFields);
public ResolvingContext(ObjectMapper mapper, KubernetesSerialization kubernetesSerialization, boolean implicitPreserveUnknownFields) {
this(mapper, kubernetesSerialization, new ConcurrentHashMap<>(), implicitPreserveUnknownFields);
}

private ResolvingContext(ObjectMapper mapper, Map<String, GeneratorObjectSchema> uriToJacksonSchema,
private ResolvingContext(ObjectMapper mapper, KubernetesSerialization kubernetesSerialization, Map<String, GeneratorObjectSchema> uriToJacksonSchema,
boolean implicitPreserveUnknownFields) {
this.uriToJacksonSchema = uriToJacksonSchema;
this.objectMapper = mapper;
this.kubernetesSerialization = kubernetesSerialization;
this.implicitPreserveUnknownFields = implicitPreserveUnknownFields;
generator = new JsonSchemaGenerator(mapper, new WrapperFactory() {

Expand Down
Loading

0 comments on commit f1b5490

Please sign in to comment.