Search in sources :

Example 1 with AbstractJSONSchema2Pojo

use of io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo in project kubernetes-client by fabric8io.

the class CRGeneratorRunner method generate.

public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, String basePackageName) {
    CustomResourceDefinitionSpec crSpec = crd.getSpec();
    String crName = crSpec.getNames().getKind();
    String group = crSpec.getGroup();
    List<WritableCRCompilationUnit> writableCUs = new ArrayList<>(crSpec.getVersions().size());
    for (CustomResourceDefinitionVersion crdv : crSpec.getVersions()) {
        String version = crdv.getName();
        String pkg = Optional.ofNullable(basePackageName).map(p -> p + "." + version).orElse(version);
        AbstractJSONSchema2Pojo specGenerator = null;
        String prefix = crName;
        if (config.getPrefixStrategy() == Config.Prefix.NEVER) {
            prefix = "";
        }
        JSONSchemaProps spec = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("spec");
        if (spec != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Spec" : "";
            specGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("spec", spec, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo statusGenerator = null;
        JSONSchemaProps status = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("status");
        if (status != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Status" : "";
            statusGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("status", status, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo crGenerator = new JCRObject(pkg, crName, group, version, prefix + "Spec", prefix + "Status", specGenerator != null, statusGenerator != null, crdv.getStorage(), crdv.getServed(), config);
        List<GeneratorResult.ClassResult> classResults = validateAndAggregate(crGenerator, specGenerator, statusGenerator);
        writableCUs.add(new WritableCRCompilationUnit(classResults));
    }
    return writableCUs;
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) Files(java.nio.file.Files) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) GeneratorResult(io.fabric8.java.generator.nodes.GeneratorResult) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Serialization(io.fabric8.kubernetes.client.utils.Serialization) FileVisitOption(java.nio.file.FileVisitOption) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) JCRObject(io.fabric8.java.generator.nodes.JCRObject) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Path(java.nio.file.Path) JCRObject(io.fabric8.java.generator.nodes.JCRObject) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec)

Example 2 with AbstractJSONSchema2Pojo

use of io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo in project kubernetes-client by fabric8io.

the class JObject method generateJava.

@Override
public GeneratorResult generateJava() {
    CompilationUnit cu = new CompilationUnit();
    if (!this.pkg.isEmpty()) {
        cu.setPackageDeclaration(this.pkg);
    }
    ClassOrInterfaceDeclaration clz = cu.addClass(this.className);
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonInclude"), new NameExpr("com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL")));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyOrder"), new NameExpr(getSortedFieldsAsParam(this.fields.keySet()))));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.databind.annotation.JsonDeserialize"), new NameExpr("using = com.fasterxml.jackson.databind.JsonDeserializer.None.class")));
    if (config.isObjectExtraAnnotations()) {
        addExtraAnnotations(clz);
    }
    clz.addImplementedType("io.fabric8.kubernetes.api.model.KubernetesResource");
    List<GeneratorResult.ClassResult> buffer = new ArrayList<>(this.fields.size() + 1);
    List<String> sortedKeys = this.fields.keySet().stream().sorted().collect(Collectors.toList());
    for (String k : sortedKeys) {
        AbstractJSONSchema2Pojo prop = this.fields.get(k);
        boolean isRequired = this.required.contains(k);
        GeneratorResult gr = prop.generateJava();
        // For now the inner types are only for enums
        if (!gr.getInnerClasses().isEmpty()) {
            for (GeneratorResult.ClassResult enumCR : gr.getInnerClasses()) {
                Optional<EnumDeclaration> ed = enumCR.getCompilationUnit().getEnumByName(enumCR.getName());
                if (ed.isPresent()) {
                    clz.addMember(ed.get());
                }
            }
        }
        buffer.addAll(gr.getTopLevelClasses());
        String originalFieldName = k;
        String fieldName = AbstractJSONSchema2Pojo.sanitizeString(k);
        String fieldType = prop.getType();
        try {
            FieldDeclaration objField = clz.addField(fieldType, fieldName, Modifier.Keyword.PRIVATE);
            objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonProperty"), new StringLiteralExpr(originalFieldName)));
            if (isRequired) {
                objField.addAnnotation("javax.validation.constraints.NotNull");
            }
            objField.createGetter();
            objField.createSetter();
            if (Utils.isNotNullOrEmpty(prop.getDescription())) {
                objField.setJavadocComment(prop.getDescription());
                objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyDescription"), new StringLiteralExpr(prop.getDescription().replace("\"", "\\\""))));
            }
        } catch (Exception cause) {
            throw new JavaGeneratorException("Error generating field " + fieldName + " with type " + prop.getType(), cause);
        }
    }
    if (this.preserveUnknownFields || config.isAlwaysPreserveUnknownFields()) {
        ClassOrInterfaceType mapType = new ClassOrInterfaceType().setName(Keywords.JAVA_UTIL_MAP).setTypeArguments(new ClassOrInterfaceType().setName("String"), new ClassOrInterfaceType().setName("Object"));
        FieldDeclaration objField = clz.addField(mapType, Keywords.ADDITIONAL_PROPERTIES, Modifier.Keyword.PRIVATE);
        objField.setVariables(new NodeList<>(new VariableDeclarator().setName(Keywords.ADDITIONAL_PROPERTIES).setType(mapType).setInitializer("new java.util.HashMap<>()")));
        objField.addAnnotation("com.fasterxml.jackson.annotation.JsonIgnore");
        objField.createGetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnyGetter");
        objField.createSetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnySetter");
    }
    buffer.add(new GeneratorResult.ClassResult(this.className, cu));
    return new GeneratorResult(buffer);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException)

Aggregations

JavaGeneratorException (io.fabric8.java.generator.exceptions.JavaGeneratorException)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 AbstractJSONSchema2Pojo (io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo)1 GeneratorResult (io.fabric8.java.generator.nodes.GeneratorResult)1 JCRObject (io.fabric8.java.generator.nodes.JCRObject)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)1 CustomResourceDefinitionSpec (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec)1 CustomResourceDefinitionVersion (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)1 JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps)1 Serialization (io.fabric8.kubernetes.client.utils.Serialization)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1