Skip to content

Commit

Permalink
fix(clients): highlight and snippet results e2e (#3567)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
shortcuts and millotp authored Aug 21, 2024
1 parent 3f3e462 commit 4cbbce5
Show file tree
Hide file tree
Showing 24 changed files with 341 additions and 260 deletions.
5 changes: 5 additions & 0 deletions clients/algoliasearch-client-go/algolia/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ func ParameterToString(obj any) string {

return fmt.Sprintf("%v", obj)
}

func HasKey[T any](m map[string]T, key string) bool {
_, ok := m[key]
return ok
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ internal val JsonElement.isString: Boolean
* Returns true if [JsonElement] is a [JsonArray] of primitives, false otherwise.
*/
internal val JsonElement.isJsonArrayOfPrimitives: Boolean
get() = this is JsonArray && first() is JsonPrimitive
get() = this is JsonArray && (isEmpty() || first() is JsonPrimitive)

/**
* Returns true if [JsonElement] is a [JsonArray] of objects, false otherwise.
*/
internal val JsonElement.isJsonArrayOfObjects: Boolean
get() = this is JsonArray && first() is JsonObject
get() = this is JsonArray && (isEmpty() || first() is JsonObject)
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
Map<String, ModelsMap> models = super.postProcessAllModels(objs);
OneOf.updateModelsOneOf(models, modelPackage);
GenericPropagator.propagateGenericsToModels(models);
OneOf.addOneOfMetadata(models);

for (Map.Entry<String, ModelsMap> entry : models.entrySet()) {
String modelName = entry.getKey();
Expand Down
56 changes: 29 additions & 27 deletions generators/src/main/java/com/algolia/codegen/utils/OneOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ private OneOf() {
}

public static void updateModelsOneOf(Map<String, ModelsMap> models, String modelPackage) {
// first, propagate the discriminator of allOf to the parent
for (ModelsMap modelContainer : models.values()) {
var model = modelContainer.getModels().get(0).getModel();
if (model.getComposedSchemas() != null && model.getComposedSchemas().getAllOf() != null) {
for (CodegenProperty prop : model.getComposedSchemas().getAllOf()) {
if (prop.vendorExtensions.containsKey("x-discriminator-fields")) {
model.vendorExtensions.put("x-discriminator-fields", prop.vendorExtensions.get("x-discriminator-fields"));
}
}
}
}

for (ModelsMap modelContainer : models.values()) {
// modelContainers always have 1 and only 1 model in our specs
var model = modelContainer.getModels().get(0).getModel();
Expand Down Expand Up @@ -63,7 +75,6 @@ private static void markOneOfChildren(Map<String, ModelsMap> models, CodegenMode
markCompounds(models, oneOf, oneOfModel, model);
oneOfList.add(oneOfModel);
}
oneOfList.sort(comparator); // have fields with "discriminators" in the start of the list
model.vendorExtensions.put("x-one-of-list", oneOfList);
}

Expand All @@ -86,8 +97,13 @@ public static void markCompounds(Map<String, ModelsMap> models, String oneOf, Ma
//noinspection unchecked
var values = (List<String>) compoundModel.vendorExtensions.get("x-discriminator-fields");
if (values != null) {
List<Map<String, String>> newValues = values.stream().map(value -> Collections.singletonMap("field", value)).toList();
oneOfModel.put("discriminators", newValues);
oneOfModel.put("x-discriminator-fields", values);
// find the matching composed schema and assign the discriminator
for (var m : model.getComposedSchemas().getOneOf()) {
if (m.openApiType.equals(compoundModel.classname)) {
m.vendorExtensions.put("x-discriminator-fields", values);
}
}
}
}

Expand All @@ -104,29 +120,6 @@ private static boolean isNumberType(String typeName) {
return typeName.equals("Int") || typeName.equals("Double") || typeName.equals("Long");
}

public static final Comparator<Map<String, Object>> comparator = (mapA, mapB) -> {
boolean hasDiscriminatorA = mapA.containsKey("discriminators");
boolean hasDiscriminatorB = mapB.containsKey("discriminators");
// Maps with "discriminators" come first
if (hasDiscriminatorA && !hasDiscriminatorB) {
return -1;
} 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;
}
};

/**
* Add metadata about oneOfs models (e.g., if it has at least one model, if it has more than one
* array-subtype, etc.)
Expand All @@ -139,6 +132,7 @@ public static void addOneOfMetadata(Map<String, ModelsMap> models) {
if (isMultiArrayOneOfs(oneOfs)) model.vendorExtensions.put("x-is-multi-array", true);
if (isMultiMapOneOfs(oneOfs)) model.vendorExtensions.put("x-is-multi-map", true);
if (hasAtModelOrEnum(oneOfs)) model.vendorExtensions.put("x-has-model", true);
if (hasDiscriminators(oneOfs)) model.vendorExtensions.put("x-has-discriminator", true);
markOneOfModels(oneOfs);
sortOneOfs(oneOfs);
}
Expand Down Expand Up @@ -178,6 +172,14 @@ private static boolean hasAtModelOrEnum(List<CodegenProperty> oneOfs) {
return false;
}

/** Return true if at least one oneOf has discriminators */
private static boolean hasDiscriminators(List<CodegenProperty> oneOfs) {
for (var prop : oneOfs) {
if (prop.vendorExtensions.containsKey("x-discriminator-fields")) return true;
}
return false;
}

/** Mark oneOf models */
private static void markOneOfModels(List<CodegenProperty> oneOfs) {
for (var prop : oneOfs) {
Expand All @@ -201,7 +203,7 @@ private static void sortOneOfs(List<CodegenProperty> oneOfs) {
return 1;
} else if (hasDiscriminatorA && hasDiscriminatorB) {
List<?> discriminatorsA = (List<?>) propA.vendorExtensions.get("x-discriminator-fields");
List<?> discriminatorsB = (List<?>) propA.vendorExtensions.get("x-discriminator-fields");
List<?> discriminatorsB = (List<?>) propB.vendorExtensions.get("x-discriminator-fields");
return discriminatorsB.size() - discriminatorsA.size();
} else {
return 0;
Expand Down
5 changes: 0 additions & 5 deletions specs/abtesting/common/schemas/ABTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ ABTest:
- createdAt
- endAt
- updatedAt
- conversionSignificance
- clickSignificance
- purchaseSignificance
- addToCartSignificance
- revenueSignificance
- abTestID
- variants

Expand Down
2 changes: 2 additions & 0 deletions specs/abtesting/common/schemas/AddABTestsVariant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ customSearchParams:
type: object
required:
- customSearchParameters
x-discriminator-fields:
- customSearchParameters
5 changes: 0 additions & 5 deletions specs/abtesting/common/schemas/Variant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ variant:
- noResultCount
- index
- description
- conversionRate
- conversionCount
- clickThroughRate
- clickCount
- averageClickPosition
- addToCartRate
- addToCartCount
- purchaseRate
- purchaseCount
3 changes: 3 additions & 0 deletions specs/common/schemas/HighlightResult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ highlightResultOption:
- value
- matchLevel
- matchedWords
x-discriminator-fields:
- matchLevel
- matchedWords

highlightedValue:
type: string
Expand Down
2 changes: 2 additions & 0 deletions specs/common/schemas/SnippetResult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ snippetResultOption:
required:
- value
- matchLevel
x-discriminator-fields:
- matchLevel

snippetResultOptionMap:
type: object
Expand Down
20 changes: 20 additions & 0 deletions specs/ingestion/common/schemas/authentication.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ AuthGoogleServiceAccountPartial:
privateKey:
type: string
description: Private key of the Google service account. This field is `null` in the API response.
x-discriminator-fields:
- clientEmail

AuthBasicPartial:
type: object
Expand All @@ -139,6 +141,8 @@ AuthBasicPartial:
password:
type: string
description: Password. This field is `null` in the API response.
x-discriminator-fields:
- username

AuthAPIKeyPartial:
type: object
Expand All @@ -148,6 +152,8 @@ AuthAPIKeyPartial:
key:
type: string
description: API key. This field is `null` in the API response.
x-discriminator-fields:
- key

AuthOAuthPartial:
type: object
Expand All @@ -167,6 +173,8 @@ AuthOAuthPartial:
type: string
default: ''
description: OAuth scope.
x-discriminator-fields:
- url

AuthAlgoliaPartial:
type: object
Expand Down Expand Up @@ -211,6 +219,9 @@ AuthGoogleServiceAccount:
required:
- clientEmail
- privateKey
x-discriminator-fields:
- clientEmail
- privateKey

AuthBasic:
type: object
Expand All @@ -226,6 +237,9 @@ AuthBasic:
required:
- username
- password
x-discriminator-fields:
- username
- password

AuthAPIKey:
type: object
Expand All @@ -237,6 +251,8 @@ AuthAPIKey:
description: API key. This field is `null` in the API response.
required:
- key
x-discriminator-fields:
- key

AuthOAuth:
type: object
Expand All @@ -260,6 +276,10 @@ AuthOAuth:
- url
- client_id
- client_secret
x-discriminator-fields:
- url
- client_id
- client_secret

AuthAlgolia:
type: object
Expand Down
7 changes: 7 additions & 0 deletions specs/ingestion/common/schemas/source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ SourceDocker:
- image
- imageType
- configuration
x-discriminator-fields:
- registry
- image
- imageType
- configuration

SourceUpdateDocker:
type: object
Expand All @@ -387,6 +392,8 @@ SourceUpdateDocker:
description: Configuration of the spec.
required:
- configuration
x-discriminator-fields:
- configuration

DockerRegistry:
type: string
Expand Down
9 changes: 9 additions & 0 deletions specs/ingestion/common/schemas/task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ ScheduleTriggerInput:
required:
- type
- cron
x-discriminator-fields:
- cron

ScheduleTrigger:
type: object
Expand Down Expand Up @@ -444,6 +446,9 @@ MappingInput:
required:
- format
- actions
x-discriminator-fields:
- format
- actions

StreamingInput:
type: object
Expand All @@ -454,6 +459,8 @@ StreamingInput:
$ref: '#/MappingInput'
required:
- mapping
x-discriminator-fields:
- mapping

DockerStreamsInput:
description: The selected streams of a singer or airbyte connector.
Expand All @@ -465,6 +472,8 @@ DockerStreamsInput:
$ref: '#/DockerStreams'
required:
- streams
x-discriminator-fields:
- streams

DockerStreams:
type: object
Expand Down
4 changes: 4 additions & 0 deletions specs/search/paths/rules/common/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ promoteObjectID:
required:
- position
- objectID
x-discriminator-fields:
- objectID

promoteObjectIDs:
title: objectIDs
Expand All @@ -174,6 +176,8 @@ promoteObjectIDs:
required:
- position
- objectIDs
x-discriminator-fields:
- objectIDs

promotePosition:
type: integer
Expand Down
Loading

0 comments on commit 4cbbce5

Please sign in to comment.