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 abe9a2a
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 238 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 @@ -226,7 +224,7 @@ public PropertyMetadata(JsonSchema value, BeanProperty beanProperty) {
if (value.isStringSchema()) {
StringSchema stringSchema = value.asStringSchema();
// only set if ValidationSchemaFactoryWrapper is used
this.pattern = stringSchema.getPattern(); // looks for the Pattern annotation
this.pattern = stringSchema.getPattern();
this.max = ofNullable(stringSchema.getMaxLength()).map(Integer::doubleValue).orElse(null);
this.min = ofNullable(stringSchema.getMinLength()).map(Integer::doubleValue).orElse(null);
} else {
Expand All @@ -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 @@ -15,15 +15,11 @@
*/
package io.fabric8.crdv2.generator;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,6 +29,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 @@ -46,10 +43,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static com.fasterxml.jackson.annotation.JsonInclude.Value.construct;

public class CRDGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(CRDGenerator.class);
Expand All @@ -58,20 +51,9 @@ 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
public static final ObjectMapper YAML_MAPPER = JsonMapper.builder(new YAMLFactory()
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
.enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER))
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(SerializationFeature.INDENT_OUTPUT, true)
.withConfigOverride(Map.class, configOverride -> configOverride.setInclude(construct(NON_NULL, NON_NULL)))
.serializationInclusion(NON_EMPTY)
.build();

public CRDGenerator inOutputDir(File outputDir) {
output = new DirCRDOutput(outputDir);
return this;
Expand All @@ -92,8 +74,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 @@ -172,8 +155,9 @@ public CRDGenerationInfo detailedGenerate() {
final ResolvingContext context;
if (this.objectMapper == null) {
context = ResolvingContext.defaultResolvingContext(implicitPreserveUnknownFields);
this.kubernetesSerialization = context.kubernetesSerialization;
} 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 +192,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 = kubernetesSerialization.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,25 @@ 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 abe9a2a

Please sign in to comment.