Skip to content

Commit b07e78d

Browse files
authored
feat(python): re-export models from __init__.py files (#4923)
1 parent c9914b1 commit b07e78d

File tree

12 files changed

+93
-29
lines changed

12 files changed

+93
-29
lines changed

generators/src/main/java/com/algolia/codegen/AlgoliaCSharpGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void processOpenAPI(OpenAPI openAPI) {
134134
@Override
135135
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
136136
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
137-
ModelPruner.removeOrphans(this, operations, models);
137+
ModelPruner.removeOrphanModelFiles(this, operations, models);
138138
Helpers.removeHelpers(operations);
139139
GenericPropagator.propagateGenericsToOperations(operations, models);
140140
return operations;

generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
140140
@Override
141141
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
142142
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
143-
ModelPruner.removeOrphans(this, operations, models);
143+
ModelPruner.removeOrphanModelFiles(this, operations, models);
144144
Helpers.removeHelpers(operations);
145145
GenericPropagator.propagateGenericsToOperations(operations, models);
146146
return operations;

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
9393
@Override
9494
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
9595
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
96-
ModelPruner.removeOrphans(this, operations, models);
96+
ModelPruner.removeOrphanModelFiles(this, operations, models);
9797
Helpers.removeHelpers(operations);
9898
GenericPropagator.propagateGenericsToOperations(operations, models);
9999
return operations;

generators/src/main/java/com/algolia/codegen/AlgoliaKotlinGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private static void jsonParent(Map<String, ModelsMap> models) {
190190
@Override
191191
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
192192
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
193-
ModelPruner.removeOrphans(this, operations, models);
193+
ModelPruner.removeOrphanModelFiles(this, operations, models);
194194
Helpers.removeHelpers(operations);
195195
GenericPropagator.propagateGenericsToOperations(operations, models);
196196
return operations;

generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
8787
@Override
8888
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
8989
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
90-
ModelPruner.removeOrphans(this, operations, models);
90+
ModelPruner.removeOrphanModelFiles(this, operations, models);
9191
Helpers.removeHelpers(operations);
9292
return operations;
9393
}

generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void processOpts() {
7272
file.getTemplateFile().equals("api_client.mustache") ||
7373
file.getTemplateFile().equals("rest.mustache") ||
7474
file.getTemplateFile().equals("configuration.mustache") ||
75-
file.getTemplateFile().equals("__init__.model.mustache") ||
76-
file.getTemplateFile().equals("__init__.package.mustache") ||
75+
file.getTemplateFile().equals("__init__model.mustache") ||
76+
file.getTemplateFile().equals("__init__package.mustache") ||
7777
file.getTemplateFile().equals("model_anyof.mustache") ||
7878
file.getTemplateFile().equals("gitlab-ci.mustache")
7979
);
@@ -83,7 +83,7 @@ public void processOpts() {
8383
supportingFiles.add(new SupportingFile("gitignore.mustache", "../", ".gitignore"));
8484
supportingFiles.add(new SupportingFile("__init__.mustache", "", "__init__.py"));
8585
supportingFiles.add(new SupportingFile("__init__.mustache", packageName, "__init__.py"));
86-
supportingFiles.add(new SupportingFile("__init__.mustache", packageName + "/models", "__init__.py"));
86+
supportingFiles.add(new SupportingFile("__init_model__.mustache", packageName + "/models", "__init__.py"));
8787
supportingFiles.add(new SupportingFile("__init__.mustache", "http", "__init__.py"));
8888
supportingFiles.add(new SupportingFile("config.mustache", packageName, "config.py"));
8989

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

128128
List<Map<String, String>> imports = operations.getImports();
@@ -157,6 +157,11 @@ public ModelsMap postProcessModels(ModelsMap objs) {
157157
return objs;
158158
}
159159

160+
@Override
161+
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
162+
return ModelPruner.removeOrphanFromModels(this, super.postProcessSupportingFileData(objs));
163+
}
164+
160165
@Override
161166
public String toEnumDefaultValue(CodegenProperty property, String value) {
162167
// always default to None in the client, to let the server handle the default value.

generators/src/main/java/com/algolia/codegen/AlgoliaRubyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
9696
@Override
9797
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
9898
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
99-
ModelPruner.removeOrphans(this, operations, models);
99+
ModelPruner.removeOrphanModelFiles(this, operations, models);
100100
Helpers.removeHelpers(operations);
101101
return operations;
102102
}

generators/src/main/java/com/algolia/codegen/utils/ModelPruner.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ private void exploreGraph(OperationsMap operations) {
103103
}
104104
}
105105

106-
/** remove all the unused models, most likely the sub models of allOf */
107-
public static void removeOrphans(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
106+
public static List<String> getOrphanModelNames(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
108107
// visit all the models that are accessible from:
109108
// - the properties of a model (needs recursive search)
110109
// - the return type of an operation
@@ -123,6 +122,34 @@ public static void removeOrphans(CodegenConfig config, OperationsMap operations,
123122
}
124123
}
125124

125+
return toRemove;
126+
}
127+
128+
/**
129+
* remove all the unused models from the models bundle variable, to be used in pair with
130+
* `removeOrphanModelFiles`
131+
*/
132+
public static Map<String, Object> removeOrphanFromModels(CodegenConfig config, Map<String, Object> data) {
133+
var models = (List<ModelMap>) data.get("models");
134+
135+
List<String> toRemove = getOrphanModelNames(
136+
config,
137+
((Map<String, List<OperationsMap>>) data.get("apiInfo")).get("apis").get(0),
138+
models
139+
);
140+
141+
for (String modelName : toRemove) {
142+
models.removeIf(model -> config.toModelName(model.getModel().getName()).equals(modelName));
143+
}
144+
145+
data.put("models", models);
146+
147+
return data;
148+
}
149+
150+
/** remove all the unused models files, most likely the sub models of allOf */
151+
public static void removeOrphanModelFiles(CodegenConfig config, OperationsMap operations, List<ModelMap> allModels) {
152+
List<String> toRemove = getOrphanModelNames(config, operations, allModels);
126153
String templateName = config.modelTemplateFiles().keySet().iterator().next();
127154

128155
for (String modelName : toRemove) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{> __init__}}
2+
3+
{{{import}}}
4+
5+
{{#models}}
6+
{{#model}}
7+
from .{{classFilename}} import {{classname}}
8+
{{/model}}
9+
{{/models}}
10+
11+
{{#isSearchClient}}
12+
from .replace_all_objects_response import ReplaceAllObjectsResponse
13+
from .secured_api_key_restrictions import SecuredApiKeyRestrictions
14+
{{/isSearchClient}}
15+
16+
__all__ = (
17+
{{#isSearchClient}}
18+
"ReplaceAllObjectsResponse",
19+
"SecuredApiKeyRestrictions",
20+
{{/isSearchClient}}
21+
{{#models}}
22+
{{#model}}
23+
"{{classname}}",
24+
{{/model}}
25+
{{/models}}
26+
)

templates/python/api.mustache

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
{{> imports}}
33

44
{{#isSearchClient}}
5-
from algoliasearch.search.models.action import Action
6-
from algoliasearch.search.models.batch_request import BatchRequest
7-
from algoliasearch.search.models.browse_params_object import BrowseParamsObject
8-
from algoliasearch.search.models.operation_type import OperationType
9-
from algoliasearch.search.models.replace_all_objects_response import ReplaceAllObjectsResponse
10-
from algoliasearch.search.models.scope_type import ScopeType
11-
from algoliasearch.search.models.secured_api_key_restrictions import SecuredApiKeyRestrictions
12-
13-
from algoliasearch.ingestion.models.watch_response import WatchResponse
5+
from algoliasearch.search.models import (
6+
Action,
7+
BatchRequest,
8+
BrowseParamsObject,
9+
OperationType,
10+
ReplaceAllObjectsResponse,
11+
ScopeType,
12+
SecuredApiKeyRestrictions,
13+
)
14+
15+
from algoliasearch.ingestion.models import WatchResponse
1416
from algoliasearch.ingestion.config import IngestionConfig
1517
from algoliasearch.ingestion.client import (IngestionClient, IngestionClientSync)
1618
{{/isSearchClient}}
1719

1820
{{#operations}}{{#operation}}{{#imports}}
19-
from algoliasearch.{{packageName}}.models.{{#lambda.snakecase}}{{{.}}}{{/lambda.snakecase}} import {{{.}}}
21+
from algoliasearch.{{packageName}}.models import {{{.}}}
2022
{{/imports}}{{/operation}}{{/operations}}
2123

2224

0 commit comments

Comments
 (0)