Skip to content

Commit

Permalink
feat(openapi): initial support for interfaces in Maven Plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored Oct 24, 2024
1 parent 838c57a commit 7384ef6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func processProtobufOneof(_ *generator.Context, pkg *types.Package, t *types.Typ
}
if reflect.ValueOf(t.Methods).MapKeys()[0].String() == reflect.ValueOf(candidateType.Methods).MapKeys()[0].String() {
t.CommentLines = append(t.CommentLines, "+k8s:openapi-gen=x-kubernetes-fabric8-implements:"+publicInterfaceName(candidateType.Name.Name))
addedImplementation := false
for i, candidateCommentLine := range candidateType.CommentLines {
if strings.HasPrefix(candidateCommentLine, "+k8s:openapi-gen=x-kubernetes-fabric8-implementation") {
candidateType.CommentLines[i] = candidateCommentLine +","+t.Name.Name
addedImplementation = true
break
}
}
if !addedImplementation {
candidateType.CommentLines = append(candidateType.CommentLines, "+k8s:openapi-gen=x-kubernetes-fabric8-implementation:"+t.Name.Name)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ var ChaosMeshPackagePatterns = []string{

var IstioPackagePatterns = []string{
"istio.io/api/analysis/v...",
"istio.io/api/extensions/v...",
"istio.io/api/meta/v...",
"istio.io/api/networking/v...",
"istio.io/api/security/v...",
"istio.io/api/telemetry/v...",
"istio.io/api/type/v...",
"istio.io/client-go/pkg/apis/extensions/v...",
"istio.io/client-go/pkg/apis/networking/v...",
"istio.io/api/networking/v...",
"istio.io/client-go/pkg/apis/security/v...",
"istio.io/client-go/pkg/apis/telemetry/v...",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ private void processTemplate(TemplateContext ret) {
}
ret.addImport("com.fasterxml.jackson.annotation.JsonInclude");
ret.put("classJsonInclude", "NON_NULL");
ret.put("classInterface", ret.isInterface() ? "interface" : "class");
ret.put("className", ret.getClassSimpleName());
ret.put("implementsExtends", ret.isInterface() ? "extends" : "implements");
ret.put("implementedInterfaces", resolveImplementedInterfaces(ret));
final List<Map<String, Object>> templateFields = templateFields(ret);
ret.put("fields", templateFields);
Expand All @@ -140,15 +142,19 @@ private void processTemplate(TemplateContext ret) {
}
ret.put("propertyOrder", SchemaUtils.propertyOrder(ret.getClassSchema()));
ret.put("builderPackage", settings.getBuilderPackage());
if (settings.isAddBuildableReferences()) {
if (!ret.isInterface() && settings.isAddBuildableReferences()) {
ret.put("buildable", false);
ret.addImport("io.sundr.builder.annotations.Buildable");
ret.addImport("io.sundr.builder.annotations.BuildableReference");
ret.put("buildableReferences", buildableReferences(ret, templateFields));
} else {
} else if (!ret.isInterface()) {
ret.addImport("io.sundr.builder.annotations.Buildable");
ret.put("buildable", true);
}
if (!ret.getSchemaProperties().containsKey("additionalProperties")) {
if (!ret.getSchemaProperties().containsKey("additionalProperties") && !ret.isInterface()) {
ret.put("additionalProperties", true);
ret.addImport("java.util.LinkedHashMap");
ret.addImport("java.util.Map");
ret.addImport("com.fasterxml.jackson.annotation.JsonAnyGetter");
ret.addImport("com.fasterxml.jackson.annotation.JsonAnySetter");
}
Expand Down Expand Up @@ -236,12 +242,14 @@ private Path resolvePackageDirectory(TemplateContext templateContext) {
}

private String resolveImplementedInterfaces(TemplateContext templateContext) {
// Editable (all classes)
templateContext.addImport(settings.getBuilderPackage() + "." + "Editable");
final StringBuilder implementedInterfaces = new StringBuilder();
implementedInterfaces.append("Editable<").append(templateContext.getClassSimpleName()).append("Builder>");
// Editable (all classes except interfaces)
if (!templateContext.isInterface()) {
templateContext.addImport(settings.getBuilderPackage() + "." + "Editable");
implementedInterfaces.append("Editable<").append(templateContext.getClassSimpleName()).append("Builder>");
implementedInterfaces.append(" , "); // TODO: weird comma introduced by jsonschema2pojo
}
// HasMetadata
implementedInterfaces.append(" , "); // TODO: weird comma introduced by jsonschema2pojo
if (templateContext.isHasMetadata()) {
if (!templateContext.isInRootPackage()) {
templateContext.addImport(settings.getHasMetadataClass());
Expand Down Expand Up @@ -312,13 +320,10 @@ private void writeFile(TemplateContext context, String fileContents) {

private static Set<String> initDefaultImports() {
return new HashSet<>(Arrays.asList(
"java.util.LinkedHashMap",
"java.util.Map",
"javax.annotation.Generated",
"com.fasterxml.jackson.annotation.JsonIgnore",
"com.fasterxml.jackson.annotation.JsonPropertyOrder",
"com.fasterxml.jackson.databind.annotation.JsonDeserialize",
"io.sundr.builder.annotations.Buildable",
"lombok.EqualsAndHashCode",
"lombok.ToString",
"lombok.experimental.Accessors"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

Expand All @@ -39,6 +40,7 @@ final class TemplateContext implements ImportManager {
private final boolean inRootPackage;
private final String classSimpleName;
private final String className;
private final boolean isInterface;
private final boolean hasMetadata;
private final String kubernetesListType;
private final Map<String, Object> context;
Expand All @@ -53,6 +55,8 @@ final class TemplateContext implements ImportManager {
inRootPackage = packageName.equals(settings.getPackageName());
classSimpleName = SchemaUtils.refToClassName(classKey);
className = packageName + "." + classSimpleName;
isInterface = classSchema.getExtensions() != null
&& Objects.equals(classSchema.getExtensions().get("x-kubernetes-fabric8-type"), "interface");
imports = new TreeSet<>(new ImportOrderComparator());
kubernetesListType = apiVersion == null ? null : schemaUtils.kubernetesListType(this, classSchema);
hasMetadata = apiVersion != null && kubernetesListType == null && schemaUtils.isHasMetadata(classSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ public String schemaToClassName(ImportManager imports, Schema<?> schema) {
return String.format("List<%s>", schemaToClassName(imports, arraySchema.getItems()));
}
if (isMap(schema)) {
imports.addImport("java.util.Map");
imports.addImport("java.util.Map"); // Type
imports.addImport("java.util.LinkedHashMap"); // Default value
final String valueType;
if (schema.getAdditionalProperties() instanceof Schema) {
valueType = schemaToClassName(imports, (Schema<?>) schema.getAdditionalProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {{.}};
/**
* {{description}}
*/{{/hasDescription}}
{{> model_class_annotations}}public class {{className}} implements {{implementedInterfaces}}
{{> model_class_annotations}}public {{classInterface}} {{className}} {{implementsExtends}} {{implementedInterfaces}}
{
{{>model_fields}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ void mapOfObjects() {
final MapSchema schema = new MapSchema();
final String result = schemaUtils.schemaToClassName(importManager, schema);
assertEquals("Map<String, Object>", result);
assertEquals("java.util.Map", importManager.getImports().iterator().next());
assertEquals(2, importManager.getImports().size());
assertTrue(importManager.getImports().contains("java.util.Map"));
assertTrue(importManager.getImports().contains("java.util.LinkedHashMap"));
}

@Test
Expand All @@ -186,7 +188,9 @@ void mapOfIntegers() {
schema.additionalProperties(new IntegerSchema());
final String result = schemaUtils.schemaToClassName(importManager, schema);
assertEquals("Map<String, Integer>", result);
assertEquals("java.util.Map", importManager.getImports().iterator().next());
assertEquals(2, importManager.getImports().size());
assertTrue(importManager.getImports().contains("java.util.Map"));
assertTrue(importManager.getImports().contains("java.util.LinkedHashMap"));
}

@Test
Expand Down

0 comments on commit 7384ef6

Please sign in to comment.