Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void processOpenAPI(OpenAPI openAPI) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(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 @@ -140,7 +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);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(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 @@ -93,7 +93,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);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(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 @@ -190,7 +190,7 @@ private static void jsonParent(Map<String, ModelsMap> models) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(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 @@ -87,7 +87,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(this, operations, models);
Helpers.removeHelpers(operations);
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void processOpts() {
file.getTemplateFile().equals("api_client.mustache") ||
file.getTemplateFile().equals("rest.mustache") ||
file.getTemplateFile().equals("configuration.mustache") ||
file.getTemplateFile().equals("__init__.model.mustache") ||
file.getTemplateFile().equals("__init__.package.mustache") ||
file.getTemplateFile().equals("__init__model.mustache") ||
file.getTemplateFile().equals("__init__package.mustache") ||
file.getTemplateFile().equals("model_anyof.mustache") ||
file.getTemplateFile().equals("gitlab-ci.mustache")
);
Expand All @@ -83,7 +83,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("gitignore.mustache", "../", ".gitignore"));
supportingFiles.add(new SupportingFile("__init__.mustache", "", "__init__.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", packageName, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", packageName + "/models", "__init__.py"));
supportingFiles.add(new SupportingFile("__init_model__.mustache", packageName + "/models", "__init__.py"));
supportingFiles.add(new SupportingFile("__init__.mustache", "http", "__init__.py"));
supportingFiles.add(new SupportingFile("config.mustache", packageName, "config.py"));

Expand Down Expand Up @@ -122,7 +122,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(this, operations, models);
Helpers.removeHelpers(operations);

List<Map<String, String>> imports = operations.getImports();
Expand Down Expand Up @@ -157,6 +157,11 @@ public ModelsMap postProcessModels(ModelsMap objs) {
return objs;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
return ModelPruner.removeOrphanFromModels(this, super.postProcessSupportingFileData(objs));
}

@Override
public String toEnumDefaultValue(CodegenProperty property, String value) {
// always default to None in the client, to let the server handle the default value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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);
ModelPruner.removeOrphans(this, operations, models);
ModelPruner.removeOrphanModelFiles(this, operations, models);
Helpers.removeHelpers(operations);
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ 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 List<String> getOrphanModelNames(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
// visit all the models that are accessible from:
// - the properties of a model (needs recursive search)
// - the return type of an operation
Expand All @@ -123,6 +122,34 @@ public static void removeOrphans(CodegenConfig config, OperationsMap operations,
}
}

return toRemove;
}

/**
* remove all the unused models from the models bundle variable, to be used in pair with
* `removeOrphanModelFiles`
*/
public static Map<String, Object> removeOrphanFromModels(CodegenConfig config, Map<String, Object> data) {
var models = (List<ModelMap>) data.get("models");

List<String> toRemove = getOrphanModelNames(
config,
(OperationsMap) ((Map<String, List>) data.get("apiInfo")).get("apis").get(0),
models
);

for (String modelName : toRemove) {
models.removeIf(model -> config.toModelName(model.getModel().getName()).equals(modelName));
}

data.put("models", models);

return data;
}

/** remove all the unused models files, most likely the sub models of allOf */
public static void removeOrphanModelFiles(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
List<String> toRemove = getOrphanModelNames(config, operations, allModels);
String templateName = config.modelTemplateFiles().keySet().iterator().next();

for (String modelName : toRemove) {
Expand Down
26 changes: 26 additions & 0 deletions templates/python/__init_model__.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{> __init__}}

{{{import}}}

{{#models}}
{{#model}}
from .{{classFilename}} import {{classname}}
{{/model}}
{{/models}}

{{#isSearchClient}}
from .replace_all_objects_response import ReplaceAllObjectsResponse
from .secured_api_key_restrictions import SecuredApiKeyRestrictions
{{/isSearchClient}}

__all__ = (
{{#isSearchClient}}
"ReplaceAllObjectsResponse",
"SecuredApiKeyRestrictions",
{{/isSearchClient}}
{{#models}}
{{#model}}
"{{classname}}",
{{/model}}
{{/models}}
)
22 changes: 12 additions & 10 deletions templates/python/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
{{> imports}}

{{#isSearchClient}}
from algoliasearch.search.models.action import Action
from algoliasearch.search.models.batch_request import BatchRequest
from algoliasearch.search.models.browse_params_object import BrowseParamsObject
from algoliasearch.search.models.operation_type import OperationType
from algoliasearch.search.models.replace_all_objects_response import ReplaceAllObjectsResponse
from algoliasearch.search.models.scope_type import ScopeType
from algoliasearch.search.models.secured_api_key_restrictions import SecuredApiKeyRestrictions

from algoliasearch.ingestion.models.watch_response import WatchResponse
from algoliasearch.search.models import (
Action,
BatchRequest,
BrowseParamsObject,
OperationType,
ReplaceAllObjectsResponse,
ScopeType,
SecuredApiKeyRestrictions,
)

from algoliasearch.ingestion.models import WatchResponse
from algoliasearch.ingestion.config import IngestionConfig
from algoliasearch.ingestion.client import (IngestionClient, IngestionClientSync)
{{/isSearchClient}}

{{#operations}}{{#operation}}{{#imports}}
from algoliasearch.{{packageName}}.models.{{#lambda.snakecase}}{{{.}}}{{/lambda.snakecase}} import {{{.}}}
from algoliasearch.{{packageName}}.models import {{{.}}}
{{/imports}}{{/operation}}{{/operations}}


Expand Down
8 changes: 5 additions & 3 deletions templates/python/tests/e2e/e2e.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ from json import loads
from algoliasearch.{{{import}}}.client import {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}
from algoliasearch.{{{import}}}.client import {{#lambda.pascalcase}}{{{client}}}Sync{{/lambda.pascalcase}}
from algoliasearch.{{{import}}}.config import {{#lambda.pascalcase}}{{clientPrefix}}Config{{/lambda.pascalcase}}
from algoliasearch.search.models.batch_response import BatchResponse
from algoliasearch.search.models.updated_at_response import UpdatedAtResponse
from algoliasearch.search.models.get_task_response import GetTaskResponse
from algoliasearch.search.models import (
BatchResponse,
UpdatedAtResponse,
GetTaskResponse,
)
from ..helpers import Helpers
from dotenv import load_dotenv

Expand Down
10 changes: 6 additions & 4 deletions templates/python/tests/requests/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ from time import time
from json import loads
from algoliasearch.http.transporter import EchoTransporter
from algoliasearch.http.transporter_sync import EchoTransporterSync
from algoliasearch.search.models.secured_api_key_restrictions import SecuredApiKeyRestrictions
from algoliasearch.{{{import}}}.client import {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}
from algoliasearch.{{{import}}}.client import {{#lambda.pascalcase}}{{{client}}}Sync{{/lambda.pascalcase}}
from algoliasearch.{{{import}}}.config import {{#lambda.pascalcase}}{{clientPrefix}}Config{{/lambda.pascalcase}}
from algoliasearch.search.models.batch_response import BatchResponse
from algoliasearch.search.models.updated_at_response import UpdatedAtResponse
from algoliasearch.search.models.get_task_response import GetTaskResponse
from algoliasearch.search.models import (
BatchResponse,
UpdatedAtResponse,
GetTaskResponse,
SecuredApiKeyRestrictions,
)

{{#modes}}
class Test{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}{{#isSyncClient}}Sync{{/isSyncClient}}:
Expand Down