Skip to content

Commit

Permalink
fix(kotlin): deserialization (#3822)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 authored Sep 23, 2024
1 parent fff990f commit 4e82751
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions scripts/cts/testServer/apiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Express, Request, Response } from 'express';

import { setupServer } from '.';

// Checks that the client sends a different API key after the first request.
function addRoutes(app: Express): void {
app.get('/check-api-key/1', (req: Request, res: Response) => {
const headerName = 'x-algolia-api-key';
Expand Down
5 changes: 5 additions & 0 deletions specs/recommend/common/schemas/RecommendationsResponse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ recommendHit:
$ref: '../../../common/schemas/Hit.yml#/distinctSeqID'
_score:
$ref: '#/recommendScore'
x-discriminator-fields:
- objectID

trendingFacetHit:
type: object
Expand All @@ -56,6 +58,9 @@ trendingFacetHit:
$ref: '../parameters.yml#/facetName'
facetValue:
$ref: '../parameters.yml#/facetValue'
x-discriminator-fields:
- facetName
- facetValue

recommendScore:
type: number
Expand Down
29 changes: 20 additions & 9 deletions templates/kotlin/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ import kotlin.jvm.JvmInline
@Serializable({{classname}}Serializer::class)
public sealed interface {{classname}} {
{{#composedSchemas.oneOf}}
{{^isModel}}
{{^isEnumRef}}
@Serializable
@JvmInline
public value class {{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Value(public val value: {{{datatypeWithEnum}}}): {{classname}}
{{/isEnumRef}}
{{/isModel}}
{{/composedSchemas.oneOf}}

public companion object {
Expand Down Expand Up @@ -62,13 +58,9 @@ public sealed interface {{classname}} {

{{^vendorExtensions.x-is-multi-array}}{{^vendorExtensions.x-is-multi-map}}
{{#composedSchemas.oneOf}}
{{^isModel}}
{{^isEnumRef}}
public fun of(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}}
}
Expand All @@ -78,14 +70,33 @@ internal class {{classname}}Serializer : JsonContentPolymorphicSerializer<{{clas
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<{{classname}}> {
return when {
{{#composedSchemas.oneOf}}
{{^isModel}}
{{^isMap}}
{{^isArray}}
{{^isEnumRef}}
{{^isInteger}}
{{^isLong}}
{{^isDouble}}
{{^isBoolean}}
{{^isPrimitiveType}}
element is JsonObject{{#vendorExtensions.x-discriminator-fields}} && element.containsKey("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}} -> {{{datatypeWithEnum}}}.serializer()
{{/isPrimitiveType}}
{{/isBoolean}}
{{/isDouble}}
{{/isLong}}
{{/isInteger}}
{{/isEnumRef}}
{{/isArray}}
{{/isMap}}
{{/isModel}}
{{#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()
element is JsonArray -> {{{classname}}}.{{#lambda.type-to-name}}{{{datatypeWithEnum}}}{{/lambda.type-to-name}}Value.serializer()
{{/isArray}}
{{#isEnumRef}}
element.isString -> {{{datatypeWithEnum}}}.serializer()
Expand Down

0 comments on commit 4e82751

Please sign in to comment.