Skip to content

Commit

Permalink
feat(specs): recursive snippets and highlights result (#3497)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Aug 9, 2024
1 parent 4fb106b commit 4c05200
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 16 deletions.
10 changes: 10 additions & 0 deletions generators/src/main/java/com/algolia/codegen/utils/OneOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public static void addOneOfMetadata(Map<String, ModelsMap> 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);
Expand All @@ -151,6 +152,15 @@ private static List<CodegenProperty> getCodegenProperties(CodegenModel model) {
return oneOfs;
}

/** Get true if a composed type has more than a one array-subtype */
private static boolean isMultiMapOneOfs(List<CodegenProperty> 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<CodegenProperty> oneOfs) {
var arrays = 0;
Expand Down
1 change: 1 addition & 0 deletions specs/common/schemas/HighlightResult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ highlightResultOptionArray:

highlightResult:
oneOf:
- $ref: '#/highlightResultMap'
- $ref: '#/highlightResultOption'
- $ref: '#/highlightResultOptionMap'
- $ref: '#/highlightResultOptionArray'
Expand Down
1 change: 1 addition & 0 deletions specs/common/schemas/SnippetResult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ snippetResultOptionArray:

snippetResult:
oneOf:
- $ref: '#/snippetResultMap'
- $ref: '#/snippetResultOption'
- $ref: '#/snippetResultOptionMap'
- $ref: '#/snippetResultOptionArray'
Expand Down
6 changes: 3 additions & 3 deletions templates/csharp/modelOneOf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
/// the InvalidClassException will be thrown{{/lambda.escape-generic}}
/// </summary>
/// <returns>An instance of {{#lambda.escape-generic}}{{dataType}}{{/lambda.escape-generic}}</returns>
public {{{dataType}}}{{#vendorExtensions.x-has-child-generic}}<T>{{/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}}<T>{{/vendorExtensions.x-has-child-generic}} As{{#lambda.titlecase}}{{baseType}}{{/lambda.titlecase}}{{#lambda.titlecase}}{{{items.dataType}}}{{/lambda.titlecase}}()
{
return ({{{dataType}}}{{#vendorExtensions.x-has-child-generic}}<T>{{/vendorExtensions.x-has-child-generic}})ActualInstance;
}
Expand All @@ -81,7 +81,7 @@
/// {{#lambda.escape-generic}}Check if the actual instance is of `{{{dataType}}}` type.{{/lambda.escape-generic}}
/// </summary>
/// <returns>Whether or not the instance is the type</returns>
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}}<T>{{/vendorExtensions.x-has-child-generic}});
}
Expand Down Expand Up @@ -243,4 +243,4 @@
writer.WriteRawValue(value.ToJson());
}
}
{{/model}}
{{/model}}
19 changes: 16 additions & 3 deletions templates/java/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}<T>{{/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}}
Expand All @@ -44,7 +57,7 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}<T>{{/ven
{{/isEnumRef}}
{{/isModel}}
{{/composedSchemas.oneOf}}
{{/vendorExtensions.x-is-multi-array}}
{{/vendorExtensions.x-is-multi-map}}{{/vendorExtensions.x-is-multi-array}}

{{#composedSchemas.oneOf}}
{{^isModel}}
Expand Down Expand Up @@ -125,4 +138,4 @@ public interface {{classname}}{{#vendorExtensions.x-has-child-generic}}<T>{{/ven
{{/isNullable}}
}
}
}
}
16 changes: 14 additions & 2 deletions templates/kotlin/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}
}
}

Expand Down
10 changes: 4 additions & 6 deletions templates/python/model_oneof.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
28 changes: 26 additions & 2 deletions templates/scala/oneof_trait.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}
Expand All @@ -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}}] {
Expand Down

0 comments on commit 4c05200

Please sign in to comment.