Skip to content

Commit

Permalink
add to all
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jul 29, 2024
1 parent 3443161 commit ae3d35d
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.algolia.codegen.exceptions.*;
import com.algolia.codegen.utils.*;
import com.algolia.codegen.utils.OneOf;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -142,6 +141,7 @@ public void processOpenAPI(OpenAPI openAPI) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.algolia.codegen.exceptions.*;
import com.algolia.codegen.lambda.ScreamingSnakeCaseLambda;
import com.algolia.codegen.utils.*;
import com.algolia.codegen.utils.OneOf;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.samskivert.mustache.Mustache;
Expand Down Expand Up @@ -132,6 +131,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models, true);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
OrphanDestroyer.removeOrphans(this, operations, models);
return operations;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.algolia.codegen;

import com.algolia.codegen.utils.*;
import com.algolia.codegen.utils.OneOf;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -188,6 +187,7 @@ private static void jsonParent(Map<String, ModelsMap> models) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);

List<Map<String, String>> imports = operations.getImports();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import static org.openapitools.codegen.utils.StringUtils.camelize;

import com.algolia.codegen.exceptions.*;
import com.algolia.codegen.utils.GenericPropagator;
import com.algolia.codegen.utils.Helpers;
import com.algolia.codegen.utils.OneOf;
import com.algolia.codegen.utils.*;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -288,6 +286,7 @@ public void processOpenAPI(OpenAPI openAPI) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
OrphanDestroyer.removeOrphans(this, operations, models);
Helpers.removeHelpers(operations);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

public class GenericPropagator {

public String language;

private static Set<String> primitiveModels = new HashSet<>(Arrays.asList("object", "array", "string", "boolean", "integer"));

// Only static use of this class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
public class OrphanDestroyer {

private static Set<String> primitiveModels = new HashSet<>(Arrays.asList("object", "array", "string", "boolean", "integer"));
private static Set<String> helperModels = new HashSet<>(
Arrays.asList("apiKeyOperation", "securedApiKeyRestrictions", "replaceAllObjectsResponse")
);

private Map<String, CodegenModel> models;
private HashSet<String> visitedModels;
Expand All @@ -21,23 +18,14 @@ private OrphanDestroyer(Map<String, CodegenModel> models) {
this.models = models;
}

private String stripType(String type) {
if (type.startsWith("List<")) {
return stripType(type.substring(5, type.length() - 1)).trim();
} else if (type.startsWith("Map<")) {
return stripType(type.substring(4, type.length() - 1).split(",", 2)[1].trim());
}
return type.trim();
}

private CodegenModel getModel(String name) {
// openapi generator returns some weird error when looking for primitive type, so we filter them
// by hand
if (primitiveModels.contains(name)) {
return null;
}

return models.get(stripType(name));
return models.get(name);
}

private CodegenModel propertyToModel(CodegenProperty prop) {
Expand Down Expand Up @@ -84,13 +72,13 @@ private void exploreGraph(OperationsMap operations) {

for (CodegenOperation ope : operations.getOperations().getOperation()) {
if (ope.returnType != null) {
CodegenModel returnType = getModel(ope.returnType);
CodegenModel returnType = getModel(ope.returnBaseType);
if (returnType != null) {
visitedModels.add(returnType.name);
}
}
for (CodegenParameter param : ope.allParams) {
CodegenModel paramType = getModel(param.dataType);
CodegenModel paramType = getModel(param.baseType != null ? param.baseType : param.dataType);
if (paramType != null) {
visitedModels.add(paramType.name);
}
Expand All @@ -103,21 +91,22 @@ private void exploreGraph(OperationsMap operations) {
}

/** remove all the unused models, most likely the sub models of allOf */
public static void removeOrphans(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
public static void removeOrphans(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels, boolean keepError) {
// visit all the models that are accessible from:
// - the properties of a model (needs recursive search)
// - the return type of an operation
// - the parameters of an operation

OrphanDestroyer orphanDestroyer = new OrphanDestroyer(convertToMap(allModels));

Helpers.prettyPrint("Models: " + orphanDestroyer.models.keySet());

orphanDestroyer.exploreGraph(operations);

List<String> toRemove = new ArrayList<>();
for (String modelName : orphanDestroyer.models.keySet()) {
if (!helperModels.contains(modelName) && !orphanDestroyer.visitedModels.contains(modelName)) {
if (keepError && modelName.equals("ErrorBase")) {
continue;
}
if (!orphanDestroyer.visitedModels.contains(modelName)) {
toRemove.add(modelName);
}
}
Expand All @@ -133,4 +122,8 @@ public static void removeOrphans(CodegenConfig config, OperationsMap operations,
}
}
}

public static void removeOrphans(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
removeOrphans(config, operations, allModels, false);
}
}

0 comments on commit ae3d35d

Please sign in to comment.