Skip to content

Commit

Permalink
fix(go): fix the spec and update the base model template (#2145)
Browse files Browse the repository at this point in the history
Co-authored-by: Mouaad Aallam <[email protected]>
  • Loading branch information
Fluf22 and aallam authored Oct 23, 2023
1 parent efdf2de commit daa9867
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 116 deletions.
8 changes: 7 additions & 1 deletion clients/algoliasearch-client-dart/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
include: package:lints/recommended.yaml

linter:
rules:
deprecated_member_use_from_same_package: false

analyzer:
exclude:
- "**/*.g.dart"
- "**/*.g.dart"
errors:
deprecated_member_use_from_same_package: ignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.algolia.codegen;

import com.algolia.codegen.exceptions.*;
import com.algolia.codegen.utils.OneOfUtils;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import java.io.File;
import java.util.List;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;
import java.util.*;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.GoClientCodegen;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;

public class AlgoliaGoGenerator extends GoClientCodegen {

Expand Down Expand Up @@ -54,4 +57,19 @@ public void processOpts() {
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}

@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
Map<String, ModelsMap> models = super.postProcessAllModels(objs);
OneOfUtils.updateModelsOneOf(models, modelPackage);
GenericPropagator.propagateGenericsToModels(models);
return models;
}

@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
GenericPropagator.propagateGenericsToOperations(operations, models);
return operations;
}
}
10 changes: 10 additions & 0 deletions generators/src/main/java/com/algolia/codegen/utils/OneOfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ private static boolean isNumberType(String typeName) {
} else if (!hasDiscriminatorA && hasDiscriminatorB) {
return 1;
} else {
// If both maps have or don't have "discriminators," compare their list lengths
if (hasDiscriminatorA && hasDiscriminatorB) {
List<?> discriminatorsA = (List<?>) mapA.get("discriminators");
List<?> discriminatorsB = (List<?>) mapB.get("discriminators");

// Compare the lengths of the lists
return discriminatorsB.size() - discriminatorsA.size();
}

// If the lengths are the same or both maps don't have "discriminators," return 0
return 0;
}
};
Expand Down
97 changes: 28 additions & 69 deletions playground/go/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,24 @@ func testSearch(appID, apiKey string) int {
indexName := getEnvWithDefault("SEARCH_INDEX", "test_index")
searchClient := search.NewClient(appID, apiKey)

searchParams := search.SearchParamsStringAsSearchParams(search.NewSearchParamsString(search.WithSearchParamsStringParams("query=jeans&hitsPerPage=2")))
_, err := searchClient.SearchSingleIndex(searchClient.NewApiSearchSingleIndexRequest(indexName).WithSearchParams(&searchParams))
if err != nil {
fmt.Printf("request error with SearchSingleIndex: %v\n", err)
return 1
}

apiKeyStruct := search.NewApiKey([]search.Acl{"search"})

addApiKeyResponse, err := searchClient.AddApiKey(searchClient.NewApiAddApiKeyRequest(apiKeyStruct))
if err != nil {
panic(err)
}

taskResponse, err := searchClient.WaitForApiKey(
addApiKeyResponse.Key,
apiKeyStruct,
"add",
nil,
nil,
nil,
response, err := searchClient.AddOrUpdateObject(
searchClient.NewApiAddOrUpdateObjectRequest(
indexName,
"1",
map[string]interface{}{
"name": "Foo",
"age": 42,
"city": "Paris",
},
),
)
if err != nil {
panic(err)
}

printResponse(taskResponse)

apiKeyStruct.SetAcl([]search.Acl{"search", "addObject"})

_, err = searchClient.UpdateApiKey(searchClient.NewApiUpdateApiKeyRequest(addApiKeyResponse.Key, apiKeyStruct))
if err != nil {
panic(err)
}

taskResponse, err = searchClient.WaitForApiKey(
addApiKeyResponse.Key,
apiKeyStruct,
"update",
_, err = searchClient.WaitForTask(
indexName,
*response.TaskID,
nil,
nil,
nil,
Expand All @@ -57,47 +36,27 @@ func testSearch(appID, apiKey string) int {
panic(err)
}

printResponse(taskResponse)

apiKeyStruct.SetAcl([]search.Acl{"search", "addObject"})

_, err = searchClient.UpdateApiKey(searchClient.NewApiUpdateApiKeyRequest(addApiKeyResponse.Key, apiKeyStruct))
if err != nil {
panic(err)
}

taskResponse, err = searchClient.WaitForApiKey(
addApiKeyResponse.Key,
apiKeyStruct,
"update",
nil,
nil,
nil,
searchResponse, err := searchClient.Search(
searchClient.NewApiSearchRequest(
search.NewSearchMethodParams(
[]search.SearchQuery{
search.SearchForHitsAsSearchQuery(
search.NewSearchForHits(
indexName,
search.WithSearchForHitsQuery("foo"),
),
),
},
),
),
)
if err != nil {
panic(err)
}

printResponse(taskResponse)

_, err = searchClient.DeleteApiKey(searchClient.NewApiDeleteApiKeyRequest(addApiKeyResponse.Key))
if err != nil {
panic(err)
for _, result := range searchResponse.Results {
fmt.Printf("Result: %v", result.SearchResponse)
}

taskResponse, err = searchClient.WaitForApiKey(
addApiKeyResponse.Key,
apiKeyStruct,
"delete",
nil,
nil,
nil,
)
if err != nil {
panic(err)
}

printResponse(taskResponse)

return 0
}
4 changes: 4 additions & 0 deletions specs/insights/common/schemas/AddedToCartObjectIDs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- eventSubtype
- objectIDs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- eventSubtype
- queryID
- objectIDs
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ClickedFilters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ required:
- index
- filters
- userToken
x-discriminator-fields:
- eventType
- filters
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ClickedObjectIDs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- objectIDs
4 changes: 4 additions & 0 deletions specs/insights/common/schemas/ClickedObjectIDsAfterSearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ required:
- positions
- queryID
- userToken
x-discriminator-fields:
- positions
- queryID
- eventType
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ConvertedFilters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ required:
- index
- filters
- userToken
x-discriminator-fields:
- eventType
- filters
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ConvertedObjectIDs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- objectIDs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ required:
- objectIDs
- queryID
- userToken
x-discriminator-fields:
- queryID
- eventType
4 changes: 4 additions & 0 deletions specs/insights/common/schemas/PurchasedObjectIDs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- eventSubtype
- objectIDs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- eventSubtype
- queryID
- objectIDs
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ViewedFilters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ required:
- index
- filters
- userToken
x-discriminator-fields:
- eventType
- filters
3 changes: 3 additions & 0 deletions specs/insights/common/schemas/ViewedObjectIDs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ required:
- index
- objectIDs
- userToken
x-discriminator-fields:
- eventType
- objectIDs
3 changes: 3 additions & 0 deletions specs/search/common/schemas/SearchQuery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ SearchForFacets:
allOf:
- $ref: '../../../common/schemas/SearchParams.yml#/searchParams'
- $ref: '#/searchForFacetsOptions'
x-discriminator-fields:
- facet
- type

searchForFacetsOptions:
type: object
Expand Down
Loading

0 comments on commit daa9867

Please sign in to comment.