From 025604e2d3a6826b113b9faae35c9576e312389c Mon Sep 17 00:00:00 2001 From: Rickard Andersson Date: Thu, 27 Oct 2022 11:31:29 +0300 Subject: [PATCH] fix(kt): fix BigInt serialization for structs --- CHANGELOG.md | 7 +++++++ gotyno-hs.cabal | 2 +- package.yaml | 2 +- src/CodeGeneration/Kotlin.hs | 18 ++++++++++++++++-- test/reference-output/basic.kt | 2 ++ test/reference-output/basicEnumeration.kt | 1 + test/reference-output/basicImport.kt | 1 + test/reference-output/basicOptional.kt | 1 + test/reference-output/basicStruct.kt | 1 + test/reference-output/basicUnion.kt | 1 + test/reference-output/genericStruct.kt | 1 + test/reference-output/genericUnion.kt | 1 + test/reference-output/generics.kt | 1 + test/reference-output/github.kt | 1 + test/reference-output/hasGeneric.kt | 1 + test/reference-output/importExample.kt | 1 + 16 files changed, 38 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b04518a..28c791d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 2.2.3 + +### Fixes + +- Fix bug where structures were not serializable in Kotlin if they had `{I,U}64` + fields. + ## 2.2.2 ### Fixes diff --git a/gotyno-hs.cabal b/gotyno-hs.cabal index 04c666b..e144fcd 100644 --- a/gotyno-hs.cabal +++ b/gotyno-hs.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: gotyno-hs -version: 2.2.2 +version: 2.2.3 synopsis: A type definition compiler supporting multiple output languages. description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders. category: Compiler diff --git a/package.yaml b/package.yaml index eef6235..24fc3a3 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: gotyno-hs -version: 2.2.2 +version: 2.2.3 synopsis: A type definition compiler supporting multiple output languages. description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders. license: BSD2 diff --git a/src/CodeGeneration/Kotlin.hs b/src/CodeGeneration/Kotlin.hs index 4ec458a..822b480 100644 --- a/src/CodeGeneration/Kotlin.hs +++ b/src/CodeGeneration/Kotlin.hs @@ -64,7 +64,8 @@ modulePrelude = "import com.fasterxml.jackson.databind.deser.std.*\n", "import java.text.ParseException\n", "import java.math.BigInteger\n", - "import kotlinx.serialization.Serializable" + "import kotlinx.serialization.Serializable\n", + "import org.gotynoDeclarations.BigIntegerSerializer" ] outputDefinition :: TypeDefinition -> Maybe Text @@ -74,7 +75,6 @@ outputDefinition (TypeDefinition name (Struct (GenericStruct typeVariables field pure $ outputGenericStruct name typeVariables fields outputDefinition (TypeDefinition name (Union typeTag unionType)) = pure $ outputUnion name typeTag unionType --- @TODO: use the type here to set the type of the enumeration outputDefinition (TypeDefinition name (Enumeration type' enumerationValues)) = pure $ outputEnumeration type' name enumerationValues outputDefinition (TypeDefinition name (UntaggedUnion unionCases)) = @@ -332,6 +332,9 @@ outputField (StructField fieldName fieldType) = [ "@get:JsonProperty(\"", unFieldName fieldName, "\")\n", + if isBigIntType fieldType + then " @Serializable(with = BigIntegerSerializer::class)\n" + else "", " val ", unFieldName fieldName, ": ", @@ -483,6 +486,17 @@ fieldTypeName (DefinitionReferenceType (DeclarationReference _moduleName' (DefinitionName definitionName))) = definitionName +isBigIntType :: FieldType -> Bool +isBigIntType (ComplexType (SliceType fieldType)) = isBigIntType fieldType +isBigIntType (ComplexType (ArrayType _size fieldType)) = isBigIntType fieldType +isBigIntType (ComplexType (OptionalType fieldType)) = isBigIntType fieldType +isBigIntType (ComplexType (PointerType fieldType)) = isBigIntType fieldType +isBigIntType (BasicType U64) = True +isBigIntType (BasicType U128) = True +isBigIntType (BasicType I64) = True +isBigIntType (BasicType I128) = True +isBigIntType _ = False + joinTypeVariables :: [TypeVariable] -> Text joinTypeVariables = fmap unTypeVariable >>> Text.intercalate ", " diff --git a/test/reference-output/basic.kt b/test/reference-output/basic.kt index 7e119e3..e4d4ef7 100644 --- a/test/reference-output/basic.kt +++ b/test/reference-output/basic.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class Basic { @Serializable @@ -20,6 +21,7 @@ data class Recruiter( @get:JsonProperty("recruiter") val recruiter: Recruiter?, @get:JsonProperty("created") + @Serializable(with = BigIntegerSerializer::class) val created: BigInteger, @get:JsonProperty("type") val type: String = "Recruiter" diff --git a/test/reference-output/basicEnumeration.kt b/test/reference-output/basicEnumeration.kt index f367ec9..e0a9b44 100644 --- a/test/reference-output/basicEnumeration.kt +++ b/test/reference-output/basicEnumeration.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class BasicEnumeration { enum class StringValues(val data: String) : java.io.Serializable { diff --git a/test/reference-output/basicImport.kt b/test/reference-output/basicImport.kt index c1e5f23..c78f346 100644 --- a/test/reference-output/basicImport.kt +++ b/test/reference-output/basicImport.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer import org.gotynoOutput.BasicStruct diff --git a/test/reference-output/basicOptional.kt b/test/reference-output/basicOptional.kt index 62d5831..b09ca44 100644 --- a/test/reference-output/basicOptional.kt +++ b/test/reference-output/basicOptional.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class BasicOptional { @Serializable diff --git a/test/reference-output/basicStruct.kt b/test/reference-output/basicStruct.kt index 87217c0..61d3199 100644 --- a/test/reference-output/basicStruct.kt +++ b/test/reference-output/basicStruct.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class BasicStruct { @Serializable diff --git a/test/reference-output/basicUnion.kt b/test/reference-output/basicUnion.kt index 1f4ef5b..ddba01e 100644 --- a/test/reference-output/basicUnion.kt +++ b/test/reference-output/basicUnion.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class BasicUnion { @Serializable diff --git a/test/reference-output/genericStruct.kt b/test/reference-output/genericStruct.kt index 15ba81f..a7fe032 100644 --- a/test/reference-output/genericStruct.kt +++ b/test/reference-output/genericStruct.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class GenericStruct { @Serializable diff --git a/test/reference-output/genericUnion.kt b/test/reference-output/genericUnion.kt index ebaf4ec..5ca13f6 100644 --- a/test/reference-output/genericUnion.kt +++ b/test/reference-output/genericUnion.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class GenericUnion { @Serializable diff --git a/test/reference-output/generics.kt b/test/reference-output/generics.kt index 91e662d..6cf583b 100644 --- a/test/reference-output/generics.kt +++ b/test/reference-output/generics.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer import org.gotynoOutput.Basic import org.gotynoOutput.HasGeneric diff --git a/test/reference-output/github.kt b/test/reference-output/github.kt index 284b3a5..a3bc4b4 100644 --- a/test/reference-output/github.kt +++ b/test/reference-output/github.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer class Github { @Serializable diff --git a/test/reference-output/hasGeneric.kt b/test/reference-output/hasGeneric.kt index 5c3bb2b..d771441 100644 --- a/test/reference-output/hasGeneric.kt +++ b/test/reference-output/hasGeneric.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer import org.gotynoDeclarations.* diff --git a/test/reference-output/importExample.kt b/test/reference-output/importExample.kt index f94c614..ee566be 100644 --- a/test/reference-output/importExample.kt +++ b/test/reference-output/importExample.kt @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.deser.std.* import java.text.ParseException import java.math.BigInteger import kotlinx.serialization.Serializable +import org.gotynoDeclarations.BigIntegerSerializer import org.gotynoOutput.Basic