From 4c052002ebc18a38babde36349873706853ce866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Fri, 9 Aug 2024 12:56:09 +0200 Subject: [PATCH] feat(specs): recursive snippets and highlights result (#3497) --- .../java/com/algolia/codegen/utils/OneOf.java | 10 +++++++ specs/common/schemas/HighlightResult.yml | 1 + specs/common/schemas/SnippetResult.yml | 1 + templates/csharp/modelOneOf.mustache | 6 ++-- templates/java/oneof_interface.mustache | 19 +++++++++++-- templates/kotlin/oneof_interface.mustache | 16 +++++++++-- templates/python/model_oneof.mustache | 10 +++---- templates/scala/oneof_trait.mustache | 28 +++++++++++++++++-- 8 files changed, 75 insertions(+), 16 deletions(-) diff --git a/generators/src/main/java/com/algolia/codegen/utils/OneOf.java b/generators/src/main/java/com/algolia/codegen/utils/OneOf.java index 303c0bd1ed..ecf4789b08 100644 --- a/generators/src/main/java/com/algolia/codegen/utils/OneOf.java +++ b/generators/src/main/java/com/algolia/codegen/utils/OneOf.java @@ -137,6 +137,7 @@ public static void addOneOfMetadata(Map models) { var model = modelContainer.getModels().get(0).getModel(); var oneOfs = getCodegenProperties(model); 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); markOneOfModels(oneOfs); sortOneOfs(oneOfs); @@ -151,6 +152,15 @@ private static List getCodegenProperties(CodegenModel model) { return oneOfs; } + /** Get true if a composed type has more than a one array-subtype */ + private static boolean isMultiMapOneOfs(List oneOfs) { + var map = 0; + for (var prop : oneOfs) { + if (prop.isMap) map++; + } + return map > 1; + } + /** Get true if a composed type has more than a one array-subtype */ private static boolean isMultiArrayOneOfs(List oneOfs) { var arrays = 0; diff --git a/specs/common/schemas/HighlightResult.yml b/specs/common/schemas/HighlightResult.yml index 55936e33ef..0ac1e69a34 100644 --- a/specs/common/schemas/HighlightResult.yml +++ b/specs/common/schemas/HighlightResult.yml @@ -46,6 +46,7 @@ highlightResultOptionArray: highlightResult: oneOf: + - $ref: '#/highlightResultMap' - $ref: '#/highlightResultOption' - $ref: '#/highlightResultOptionMap' - $ref: '#/highlightResultOptionArray' diff --git a/specs/common/schemas/SnippetResult.yml b/specs/common/schemas/SnippetResult.yml index fad934d77e..a915f48565 100644 --- a/specs/common/schemas/SnippetResult.yml +++ b/specs/common/schemas/SnippetResult.yml @@ -26,6 +26,7 @@ snippetResultOptionArray: snippetResult: oneOf: + - $ref: '#/snippetResultMap' - $ref: '#/snippetResultOption' - $ref: '#/snippetResultOptionMap' - $ref: '#/snippetResultOptionArray' diff --git a/templates/csharp/modelOneOf.mustache b/templates/csharp/modelOneOf.mustache index faeb82beae..76d285ecc6 100644 --- a/templates/csharp/modelOneOf.mustache +++ b/templates/csharp/modelOneOf.mustache @@ -65,7 +65,7 @@ /// the InvalidClassException will be thrown{{/lambda.escape-generic}} /// /// An instance of {{#lambda.escape-generic}}{{dataType}}{{/lambda.escape-generic}} - public {{{dataType}}}{{#vendorExtensions.x-has-child-generic}}{{/vendorExtensions.x-has-child-generic}} As{{#lambda.titlecase}}{{baseType}}{{/lambda.titlecase}}{{#isArray}}{{^complexType}}{{#lambda.titlecase}}{{{items.dataType}}}{{/lambda.titlecase}}{{/complexType}}{{/isArray}}() + public {{{dataType}}}{{#vendorExtensions.x-has-child-generic}}{{/vendorExtensions.x-has-child-generic}} As{{#lambda.titlecase}}{{baseType}}{{/lambda.titlecase}}{{#lambda.titlecase}}{{{items.dataType}}}{{/lambda.titlecase}}() { return ({{{dataType}}}{{#vendorExtensions.x-has-child-generic}}{{/vendorExtensions.x-has-child-generic}})ActualInstance; } @@ -81,7 +81,7 @@ /// {{#lambda.escape-generic}}Check if the actual instance is of `{{{dataType}}}` type.{{/lambda.escape-generic}} /// /// Whether or not the instance is the type - public bool Is{{#lambda.titlecase}}{{baseType}}{{/lambda.titlecase}}{{#isArray}}{{^complexType}}{{#lambda.titlecase}}{{{items.dataType}}}{{/lambda.titlecase}}{{/complexType}}{{/isArray}}() + public bool Is{{#lambda.titlecase}}{{baseType}}{{/lambda.titlecase}}{{#lambda.titlecase}}{{{items.dataType}}}{{/lambda.titlecase}}() { return ActualInstance.GetType() == typeof({{{dataType}}}{{#vendorExtensions.x-has-child-generic}}{{/vendorExtensions.x-has-child-generic}}); } @@ -243,4 +243,4 @@ writer.WriteRawValue(value.ToJson()); } } -{{/model}} +{{/model}} \ No newline at end of file diff --git a/templates/java/oneof_interface.mustache b/templates/java/oneof_interface.mustache index e9d1e286fe..ea9ee77bc6 100644 --- a/templates/java/oneof_interface.mustache +++ b/templates/java/oneof_interface.mustache @@ -33,7 +33,20 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}{{/ven {{/composedSchemas.oneOf}} {{/vendorExtensions.x-is-multi-array}} - {{^vendorExtensions.x-is-multi-array}} + {{#vendorExtensions.x-is-multi-map}} + {{#composedSchemas.oneOf}} + {{^isModel}} + {{^isEnumRef}} + // {{classname}} as {{{datatypeWithEnum}}} wrapper. + static {{classname}} of{{#isMap}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isMap}}({{{datatypeWithEnum}}} value) { + return new {{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Wrapper(value); + } + {{/isEnumRef}} + {{/isModel}} + {{/composedSchemas.oneOf}} + {{/vendorExtensions.x-is-multi-map}} + + {{^vendorExtensions.x-is-multi-array}}{{^vendorExtensions.x-is-multi-map}} {{#composedSchemas.oneOf}} {{^isModel}} {{^isEnumRef}} @@ -44,7 +57,7 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}{{/ven {{/isEnumRef}} {{/isModel}} {{/composedSchemas.oneOf}} - {{/vendorExtensions.x-is-multi-array}} + {{/vendorExtensions.x-is-multi-map}}{{/vendorExtensions.x-is-multi-array}} {{#composedSchemas.oneOf}} {{^isModel}} @@ -125,4 +138,4 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}{{/ven {{/isNullable}} } } -} +} \ No newline at end of file diff --git a/templates/kotlin/oneof_interface.mustache b/templates/kotlin/oneof_interface.mustache index e45def7419..76a2879c3b 100644 --- a/templates/kotlin/oneof_interface.mustache +++ b/templates/kotlin/oneof_interface.mustache @@ -48,7 +48,19 @@ public sealed interface {{classname}} { {{/composedSchemas.oneOf}} {{/vendorExtensions.x-is-multi-array}} - {{^vendorExtensions.x-is-multi-array}} + {{#vendorExtensions.x-is-multi-map}} + {{#composedSchemas.oneOf}} + {{^isModel}} + {{^isEnumRef}} + public fun of{{#isMap}}{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}{{/isMap}}(value: {{{datatypeWithEnum}}}): {{classname}} { + return {{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Value(value); + } + {{/isEnumRef}} + {{/isModel}} + {{/composedSchemas.oneOf}} + {{/vendorExtensions.x-is-multi-map}} + + {{^vendorExtensions.x-is-multi-array}}{{^vendorExtensions.x-is-multi-map}} {{#composedSchemas.oneOf}} {{^isModel}} {{^isEnumRef}} @@ -58,7 +70,7 @@ public sealed interface {{classname}} { {{/isEnumRef}} {{/isModel}} {{/composedSchemas.oneOf}} - {{/vendorExtensions.x-is-multi-array}} + {{/vendorExtensions.x-is-multi-map}}{{/vendorExtensions.x-is-multi-array}} } } diff --git a/templates/python/model_oneof.mustache b/templates/python/model_oneof.mustache index 2d2ae32f13..aa68f8b713 100644 --- a/templates/python/model_oneof.mustache +++ b/templates/python/model_oneof.mustache @@ -21,7 +21,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} """ Unwraps the `actual_instance` when calling the `to_json` method. """ - return self.actual_instance + return self.actual_instance if hasattr(self, "actual_instance") else self @classmethod def from_dict(cls, obj: dict) -> Self: @@ -76,8 +76,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} if self.actual_instance is None: return "null" - to_json = getattr(self.actual_instance, "to_json", None) - if callable(to_json): + if hasattr(self.actual_instance, "to_json"): return self.actual_instance.to_json() else: return dumps(self.actual_instance) @@ -87,8 +86,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} if self.actual_instance is None: return None - to_dict = getattr(self.actual_instance, "to_dict", None) - if callable(to_dict): + if hasattr(self.actual_instance, "to_dict"): return self.actual_instance.to_dict() else: - return self.actual_instance + return self.actual_instance \ No newline at end of file diff --git a/templates/scala/oneof_trait.mustache b/templates/scala/oneof_trait.mustache index 662abb52ee..957df4524b 100644 --- a/templates/scala/oneof_trait.mustache +++ b/templates/scala/oneof_trait.mustache @@ -23,6 +23,18 @@ object {{classname}}Evidence { } {{/vendorExtensions.x-is-multi-array}} +{{#vendorExtensions.x-is-multi-map}} +trait {{classname}}Evidence + +object {{classname}}Evidence { +{{#composedSchemas.oneOf}} +{{#isMap}} + implicit object {{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Evidence extends {{classname}}Evidence +{{/isMap}} +{{/composedSchemas.oneOf}} +} +{{/vendorExtensions.x-is-multi-map}} + object {{classname}} { {{#composedSchemas.oneOf}} @@ -44,8 +56,20 @@ object {{classname}} { {{/isModel}} {{/composedSchemas.oneOf}} {{/vendorExtensions.x-is-multi-array}} + + {{#vendorExtensions.x-is-multi-map}} + {{#composedSchemas.oneOf}} + {{^isModel}} + {{^isEnumRef}} + def apply(value: {{{datatypeWithEnum}}}){{#isMap}}(implicit ev: {{classname}}Evidence.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Evidence.type){{/isMap}}: {{classname}} = { + {{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}} + {{/composedSchemas.oneOf}} + {{/vendorExtensions.x-is-multi-map}} - {{^vendorExtensions.x-is-multi-array}} + {{^vendorExtensions.x-is-multi-array}}{{^vendorExtensions.x-is-multi-map}} {{#composedSchemas.oneOf}} {{^isModel}} {{^isEnumRef}} @@ -55,7 +79,7 @@ object {{classname}} { {{/isEnumRef}} {{/isModel}} {{/composedSchemas.oneOf}} - {{/vendorExtensions.x-is-multi-array}} + {{/vendorExtensions.x-is-multi-map}}{{/vendorExtensions.x-is-multi-array}} } object {{classname}}Serializer extends Serializer[{{classname}}] {