Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(specs): highlight result map definition #2312

Merged
merged 12 commits into from
Dec 4, 2023
2 changes: 1 addition & 1 deletion config/clients.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"folder": "clients/algoliasearch-client-scala",
"gitRepoId": "algoliasearch-client-scala",
"packageVersion": "2.0.0-alpha.1",
"modelFolder": "src/main/scala/algoliasearch/model",
"modelFolder": "src/main/scala/algoliasearch",
"apiFolder": "src/main/scala/algoliasearch/api",
"customGenerator": "algolia-scala",
"tests": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ public String toEnumVarName(String value, String datatype) {

/** Convert a Seq type to a valid class name. */
private String typeToName(String content) {
return content.trim().replace("<", "Of").replace(">", "");
return content.trim().replace("<", "Of").replace(">", "").replace(",", "").replace(" ", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void processOpts() {

/** Convert a Seq type to a valid class name. */
private String typeToName(String content) {
return content.trim().replace("<", "Of").replace(">", "");
return content.trim().replace("<", "Of").replace(">", "").replace(", ", "").replace(".", "");
}

private void hostForKotlin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void processOpts() {

/** Convert a Seq type to a valid class name. */
private String typeToName(String content) {
return content.trim().replace("[", "Of").replace("]", "");
return content.trim().replace("[", "Of").replace("]", "").replace(".", "").replace(", ", "");
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions specs/recommend/common/schemas/RecommendationsResponse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ recommendHit:
objectID:
$ref: '../../../common/parameters.yml#/objectID'
_highlightResult:
$ref: '../../../search/common/schemas/Hit.yml#/highlightResultMap'
$ref: '../../../search/common/schemas/Hit.yml#/highlightResult'
_snippetResult:
$ref: '../../../search/common/schemas/Hit.yml#/snippetResultMap'
$ref: '../../../search/common/schemas/Hit.yml#/snippetResult'
_rankingInfo:
$ref: '../../../search/common/schemas/Hit.yml#/rankingInfo'
_distinctSeqID:
Expand Down
44 changes: 28 additions & 16 deletions specs/search/common/schemas/Hit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ hit:
objectID:
$ref: '../../../common/parameters.yml#/objectID'
_highlightResult:
$ref: '#/highlightResultMap'
$ref: '#/highlightResult'
_snippetResult:
$ref: '#/snippetResultMap'
$ref: '#/snippetResult'
_rankingInfo:
$ref: '#/rankingInfo'
_distinctSeqID:
Expand All @@ -21,10 +21,25 @@ hit:
_distinctSeqID:
type: integer

highlightResultOptionMap:
type: object
description: Show highlighted section and words matched on a query.
additionalProperties:
$ref: '#/highlightResultOption'

highlightResultOptionOneOf:
oneOf:
- $ref: '#/highlightResultOption'
- $ref: '#/highlightResultOptionMap'

highlightResultOption:
type: object
description: Show highlighted section and words matched on a query.
additionalProperties: false
x-discriminator-fields:
- matchLevel
- value
- matchedWords
properties:
value:
$ref: '#/highlightedValue'
Expand All @@ -45,17 +60,10 @@ highlightResultOption:
- matchedWords

highlightResult:
oneOf:
- $ref: '#/highlightResultOption'
- type: array
items:
$ref: '#/highlightResultOption'

highlightResultMap:
type: object
description: Show highlighted section and words matched on a query.
additionalProperties:
$ref: '#/highlightResult'
$ref: '#/highlightResultOptionOneOf'

snippetResultOption:
type: object
Expand All @@ -70,18 +78,22 @@ snippetResultOption:
- value
- matchLevel

snippetResult:
snippetResultOptionMap:
type: object
description: Snippeted attributes show parts of the matched attributes. Only returned when attributesToSnippet is non-empty.
additionalProperties:
$ref: '#/snippetResultOption'

snippetResultOptionOneOf:
oneOf:
- $ref: '#/snippetResultOption'
- type: array
items:
$ref: '#/snippetResultOption'
- $ref: '#/snippetResultOptionMap'

snippetResultMap:
snippetResult:
type: object
description: Snippeted attributes show parts of the matched attributes. Only returned when attributesToSnippet is non-empty.
additionalProperties:
$ref: '#/snippetResult'
$ref: '#/snippetResultOptionOneOf'

rankingInfo:
type: object
Expand Down
4 changes: 2 additions & 2 deletions specs/search/paths/multiclusters/searchUserIds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ post:
type: object
properties:
userID:
$ref: '../../common/schemas/Hit.yml#/highlightResultMap'
$ref: '../../common/schemas/Hit.yml#/highlightResult'
clusterName:
$ref: '../../common/schemas/Hit.yml#/highlightResultMap'
$ref: '../../common/schemas/Hit.yml#/highlightResult'
required:
- userID
- clusterName
Expand Down
15 changes: 10 additions & 5 deletions templates/java/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.utils.CompoundType;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -82,7 +83,8 @@ public interface {{classname}} {
JsonNode tree = jp.readValueAsTree();
{{#composedSchemas.oneOf}}
// deserialize {{{datatypeWithEnum}}}
if (tree.{{#isModel}}isObject(){{#vendorExtensions.x-discriminator-fields}} && tree.has("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}}{{/isModel}}{{#isEnumRef}}isTextual(){{/isEnumRef}}{{#isArray}}isArray(){{/isArray}}{{#isInteger}}isInt(){{/isInteger}}{{#isLong}}isLong(){{/isLong}}{{#isDouble}}isDouble(){{/isDouble}}{{#isBoolean}}isBoolean(){{/isBoolean}}{{#isString}}isTextual(){{/isString}}){

if (tree.{{#isMap}}isObject(){{/isMap}}{{#isModel}}isObject(){{#vendorExtensions.x-discriminator-fields}} && tree.has("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}}{{/isModel}}{{#isEnumRef}}isTextual(){{/isEnumRef}}{{#isArray}}isArray(){{/isArray}}{{#isInteger}}isInt(){{/isInteger}}{{#isLong}}isLong(){{/isLong}}{{#isDouble}}isDouble(){{/isDouble}}{{#isBoolean}}isBoolean(){{/isBoolean}}{{#isString}}isTextual(){{/isString}}){
try(JsonParser parser = tree.traverse(jp.getCodec())) {
{{#isModel}}
return parser.readValueAs({{{datatypeWithEnum}}}.class);
Expand All @@ -94,10 +96,14 @@ public interface {{classname}} {
{{{datatypeWithEnum}}} value = parser.readValueAs(new TypeReference<{{{datatypeWithEnum}}}>(){});
return new {{classname}}.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Wrapper(value);
{{/isArray}}
{{^isModel}}{{^isEnumRef}}{{^isArray}}
{{^isModel}}{{^isEnumRef}}{{^isArray}}{{^isMap}}
{{{datatypeWithEnum}}} value = parser.readValueAs({{{datatypeWithEnum}}}.class);
return new {{classname}}.{{{datatypeWithEnum}}}Wrapper(value);
{{/isArray}}{{/isEnumRef}}{{/isModel}}
{{/isMap}}{{/isArray}}{{/isEnumRef}}{{/isModel}}
{{#isMap}}
{{{datatypeWithEnum}}} value = parser.readValueAs(new TypeReference<{{{datatypeWithEnum}}}>(){});
return new {{classname}}.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Wrapper(value);
{{/isMap}}
} catch (Exception e) {
// deserialization failed, continue
LOGGER.finest("Failed to deserialize oneOf {{{datatypeWithEnum}}} (error: " + e.getMessage() + ") (type: {{{datatypeWithEnum}}})");
Expand All @@ -120,5 +126,4 @@ public interface {{classname}} {
{{/isNullable}}
}
}
}

}
5 changes: 4 additions & 1 deletion templates/kotlin/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ internal class {{classname}}Serializer : JsonContentPolymorphicSerializer<{{clas
{{#isModel}}
element is JsonObject{{#vendorExtensions.x-discriminator-fields}} && element.containsKey("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}} -> {{{datatypeWithEnum}}}.serializer()
{{/isModel}}
{{#isMap}}
element is JsonObject -> {{classname}}.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Value.serializer()
{{/isMap}}
{{#isArray}}
element{{#items}}{{#isModel}}.isJsonArrayOfObjects{{/isModel}}{{#isPrimitiveType}}.isJsonArrayOfPrimitives{{/isPrimitiveType}}{{#isModel}}{{#isPrimitiveType}} is JsonArray{{/isPrimitiveType}}{{/isModel}}{{/items}} -> {{{classname}}}.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Value.serializer()
{{/isArray}}
Expand All @@ -94,4 +97,4 @@ internal class {{classname}}Serializer : JsonContentPolymorphicSerializer<{{clas
else -> throw AlgoliaClientException("Failed to deserialize json element: $element")
}
}
}
}
11 changes: 7 additions & 4 deletions templates/scala/oneof_trait.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object {{classname}} {
{{#composedSchemas.oneOf}}
{{^isModel}}
{{^isEnumRef}}
case class {{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}(value: {{{datatypeWithEnum}}}) extends {{classname}}
case class {{#isMap}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isMap}}{{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isMap}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}{{/isMap}}(value: {{{datatypeWithEnum}}}) extends {{classname}}
{{/isEnumRef}}
{{/isModel}}
{{/composedSchemas.oneOf}}
Expand All @@ -38,7 +38,7 @@ object {{classname}} {
{{^isModel}}
{{^isEnumRef}}
def apply(value: {{{datatypeWithEnum}}}){{#isArray}}(implicit ev: {{classname}}Evidence.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Evidence.type){{/isArray}}: {{classname}} = {
{{classname}}.{{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}(value)
{{classname}}.{{#isMap}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isMap}}{{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isMap}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}{{/isMap}}(value)
}
{{/isEnumRef}}
{{/isModel}}
Expand All @@ -50,7 +50,7 @@ object {{classname}} {
{{^isModel}}
{{^isEnumRef}}
def apply(value: {{{datatypeWithEnum}}}): {{classname}} = {
{{classname}}.{{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}(value)
{{classname}}.{{#isMap}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isMap}}{{#isArray}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isArray}}{{^isMap}}{{^isArray}}{{{datatypeWithEnum}}}Value{{/isArray}}{{/isMap}}(value)
}
{{/isEnumRef}}
{{/isModel}}
Expand All @@ -67,6 +67,9 @@ object {{classname}}Serializer extends Serializer[{{classname}}] {
{{#isModel}}
case value: JObject {{#vendorExtensions.x-discriminator-fields}}{{#-first}}if{{/-first}}{{^-first}}&&{{/-first}} value.obj.contains("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}} => Extraction.extract[{{{datatypeWithEnum}}}](value)
{{/isModel}}
{{#isMap}}
case value: JObject => {{classname}}.apply(Extraction.extract[{{{datatypeWithEnum}}}](value))
{{/isMap}}
{{#isEnumRef}}
case value: JString => Extraction.extract[{{{datatypeWithEnum}}}](value)
{{/isEnumRef}}
Expand Down Expand Up @@ -96,4 +99,4 @@ object {{classname}}Serializer extends Serializer[{{classname}}] {
override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case value => Extraction.decompose(value)(format - this)
}
}
}