From 1d9406883054fe061088507c8482aa2b42a192b5 Mon Sep 17 00:00:00 2001 From: Matt Riben Date: Mon, 20 Jan 2025 10:38:48 -0600 Subject: [PATCH] feat: java-generator support for required spec and status (6807) feat: java-generator support for required spec and status Signed-off-by: Matt Riben --- Merge branch 'main' into iss_6802 Co-authored-by: Andrea Peruffo --- CHANGELOG.md | 1 + .../java/generator/nodes/JCRObject.java | 21 +++++++++ .../fabric8/java/generator/nodes/JObject.java | 2 +- .../fabric8/java/generator/ApprovalTest.java | 4 +- ...s.testRequireSpecAndStatusCrd.approved.txt | 40 +++++++++++++++++ .../resources/require-spec-and-status-crd.yml | 43 +++++++++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testRequireSpecAndStatusCrd.approved.txt create mode 100644 java-generator/core/src/test/resources/require-spec-and-status-crd.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b553542a1..f720802e8e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ #### Dependency Upgrade #### New Features +* Fix #6802: Java generator support for required spec and status * Fix #5993: Support for Kubernetes v1.31 (elli) * Fix #6767: Support for Kubernetes v1.32 (penelope) diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JCRObject.java b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JCRObject.java index 4e7a7a2c769..706a560035a 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JCRObject.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JCRObject.java @@ -16,12 +16,19 @@ package io.fabric8.java.generator.nodes; import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.PackageDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.expr.Name; import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; import com.github.javaparser.ast.expr.StringLiteralExpr; +import com.github.javaparser.ast.expr.SuperExpr; +import com.github.javaparser.ast.stmt.BlockStmt; +import com.github.javaparser.ast.stmt.ReturnStmt; import com.github.javaparser.ast.type.ClassOrInterfaceType; import io.fabric8.java.generator.Config; import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; @@ -115,11 +122,25 @@ public GeneratorResult generateJava() { ? new ClassOrInterfaceType().setName(type + "Spec") : jlVoid; fields.remove("spec"); + if (required.contains("spec")) { + clz.addMethod("getSpec", Modifier.Keyword.PUBLIC) + .setType(spec) + .setBody(new BlockStmt().addStatement(new ReturnStmt(new MethodCallExpr(new SuperExpr(), "getSpec")))) + .addAnnotation(new NormalAnnotationExpr(new Name("java.lang.Override"), new NodeList<>())) + .addAnnotation(new NormalAnnotationExpr(new Name("io.fabric8.generator.annotation.Required"), new NodeList<>())); + } ClassOrInterfaceType status = (fields.containsKey("status")) ? new ClassOrInterfaceType().setName(type + "Status") : jlVoid; fields.remove("status"); + if (required.contains("status")) { + clz.addMethod("getStatus", Modifier.Keyword.PUBLIC) + .setType(status) + .setBody(new BlockStmt().addStatement(new ReturnStmt(new MethodCallExpr(new SuperExpr(), "getStatus")))) + .addAnnotation(new NormalAnnotationExpr(new Name("java.lang.Override"), new NodeList<>())) + .addAnnotation(new NormalAnnotationExpr(new Name("io.fabric8.generator.annotation.Required"), new NodeList<>())); + } ClassOrInterfaceType crType = new ClassOrInterfaceType() .setName("io.fabric8.kubernetes.client.CustomResource") diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JObject.java b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JObject.java index 64652948b86..de97596b74b 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JObject.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JObject.java @@ -48,7 +48,7 @@ public class JObject extends AbstractJSONSchema2Pojo implements JObjectExtraAnno protected final String className; protected final String pkg; protected final Map fields; - private final Set required; + protected final Set required; private final Set deprecated = new HashSet<>(); private final boolean preserveUnknownFields; diff --git a/java-generator/core/src/test/java/io/fabric8/java/generator/ApprovalTest.java b/java-generator/core/src/test/java/io/fabric8/java/generator/ApprovalTest.java index 60a131615e8..53f88dea5e8 100644 --- a/java-generator/core/src/test/java/io/fabric8/java/generator/ApprovalTest.java +++ b/java-generator/core/src/test/java/io/fabric8/java/generator/ApprovalTest.java @@ -48,7 +48,9 @@ private static Stream getCRDGenerationInputData() { Arguments.of("testCalicoIPPoolCrd", "calico-ippool-crd.yml", "IPPool", "CalicoIPPoolCr", new Config()), Arguments.of("testExistingJavaType", "existing-java-type-crd.yml", "ExistingJavaType", "ExistingJavaTypeCr", Config.builder().existingJavaTypes(Collections.singletonMap( - "org.test.v1.existingjavatypespec.Affinity", "io.fabric8.kubernetes.api.model.Affinity")).build())); + "org.test.v1.existingjavatypespec.Affinity", "io.fabric8.kubernetes.api.model.Affinity")).build()), + Arguments.of("testRequireSpecAndStatusCrd", "require-spec-and-status-crd.yml", "RequireSpecAndStatus", + "RequireSpecAndStatusJavaCr", new Config())); } @ParameterizedTest diff --git a/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testRequireSpecAndStatusCrd.approved.txt b/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testRequireSpecAndStatusCrd.approved.txt new file mode 100644 index 00000000000..20ab8e7a8fa --- /dev/null +++ b/java-generator/core/src/test/resources/io/fabric8/java/generator/approvals/ApprovalTest.generate_withValidCrd_shouldGeneratePojos.testRequireSpecAndStatusCrd.approved.txt @@ -0,0 +1,40 @@ +RequireSpecAndStatusJavaCr[0] = package org.test.v1; + +@io.fabric8.kubernetes.model.annotation.Version(value = "v1" , storage = true , served = true) +@io.fabric8.kubernetes.model.annotation.Group("stable.example.com") +@io.fabric8.kubernetes.model.annotation.Singular("requirespecandstatus") +@io.fabric8.kubernetes.model.annotation.Plural("requirespecandstatuses") +@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner") +public class RequireSpecAndStatus extends io.fabric8.kubernetes.client.CustomResource implements io.fabric8.kubernetes.api.model.Namespaced { + + @java.lang.Override() + @io.fabric8.generator.annotation.Required() + public org.test.v1.RequireSpecAndStatusSpec getSpec() { + return super.getSpec(); + } + + @java.lang.Override() + @io.fabric8.generator.annotation.Required() + public org.test.v1.RequireSpecAndStatusStatus getStatus() { + return super.getStatus(); + } +} + +RequireSpecAndStatusJavaCr[1] = package org.test.v1; + +@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL) +@com.fasterxml.jackson.annotation.JsonPropertyOrder({}) +@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner") +public class RequireSpecAndStatusSpec implements io.fabric8.kubernetes.api.model.KubernetesResource { +} + +RequireSpecAndStatusJavaCr[2] = package org.test.v1; + +@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL) +@com.fasterxml.jackson.annotation.JsonPropertyOrder({}) +@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner") +public class RequireSpecAndStatusStatus implements io.fabric8.kubernetes.api.model.KubernetesResource { +} + diff --git a/java-generator/core/src/test/resources/require-spec-and-status-crd.yml b/java-generator/core/src/test/resources/require-spec-and-status-crd.yml new file mode 100644 index 00000000000..59b8e4b4f7e --- /dev/null +++ b/java-generator/core/src/test/resources/require-spec-and-status-crd.yml @@ -0,0 +1,43 @@ +# +# 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. +# + +# java-package:org.sample +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: requirespecandstatus.stable.example.com +spec: + group: stable.example.com + scope: Namespaced + names: + plural: requirespecandstatuses + singular: requirespecandstatus + kind: RequireSpecAndStatus + versions: + - name: v1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + required: + - spec + - status + properties: + spec: + type: object + status: + type: object