From c228024bedc718382e5d45c2b274b345712873d7 Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Tue, 26 Nov 2024 20:01:25 +0900 Subject: [PATCH] Fix typelizer options merging in inherited serializers --- CHANGELOG.md | 1 + lib/typelizer/dsl.rb | 4 +++- spec/__snapshots__/AlbaInline.ts.snap | 5 +++-- spec/__snapshots__/AlbaMetaNil.ts.snap | 5 +++-- spec/__snapshots__/AlbaUser.ts.snap | 5 +++-- spec/__snapshots__/AlbaUserAuthor.ts.snap | 5 +++-- spec/__snapshots__/AlbaUserEmptyNested.ts.snap | 5 +++-- spec/__snapshots__/AlbaUserSerializerFoo.ts.snap | 5 +++-- spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap | 5 +++-- spec/__snapshots__/AmsUser.ts.snap | 5 +++-- spec/__snapshots__/AmsUserAuthor.ts.snap | 5 +++-- spec/__snapshots__/AmsUserEmptyNested.ts.snap | 5 +++-- spec/__snapshots__/AmsUserSerializerFoo.ts.snap | 5 +++-- spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap | 5 +++-- spec/app/app/serializers/alba/base_serializer.rb | 5 +++++ spec/app/app/serializers/alba/user_serializer.rb | 4 ++++ spec/app/app/serializers/ams/base_serializer.rb | 5 +++++ spec/app/app/serializers/ams/user_serializer.rb | 4 ++++ 18 files changed, 58 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87cd145..de6a758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning]. - Do not override `Typelizer.dirs` in the railtie initializer ([@patvice]) - Do not raise on empty nested serializers ([@skryukov]) +- Attribute options merging in inherited serializers ([@Envek]) ## [0.1.5] - 2024-10-07 diff --git a/lib/typelizer/dsl.rb b/lib/typelizer/dsl.rb index 2b6d6aa..935bc98 100644 --- a/lib/typelizer/dsl.rb +++ b/lib/typelizer/dsl.rb @@ -66,7 +66,9 @@ def assign_type_information(attribute_name, attributes) define_singleton_method(attribute_name) do result = instance_variable_get(instance_variable) || {} if superclass.respond_to?(attribute_name) - result.merge(superclass.send(attribute_name)) + result.merge(superclass.send(attribute_name)) do |key, currentdef, supervaldef| + supervaldef.merge(currentdef) + end else result end diff --git a/spec/__snapshots__/AlbaInline.ts.snap b/spec/__snapshots__/AlbaInline.ts.snap index 8a01e7b..c5be7b6 100644 --- a/spec/__snapshots__/AlbaInline.ts.snap +++ b/spec/__snapshots__/AlbaInline.ts.snap @@ -1,9 +1,10 @@ -// Typelizer digest 178ddab3063fd4d03443d5febfa7c551 +// Typelizer digest 4387ed2156b17d770db0059cae5d7a38 // // DO NOT MODIFY: This file was automatically generated by Typelizer. type AlbaInline = { - id: number; + /** Unique identifier */ + id: string; username: string; active: boolean; untyped_posts: Array<{ diff --git a/spec/__snapshots__/AlbaMetaNil.ts.snap b/spec/__snapshots__/AlbaMetaNil.ts.snap index cdd28af..3f176f0 100644 --- a/spec/__snapshots__/AlbaMetaNil.ts.snap +++ b/spec/__snapshots__/AlbaMetaNil.ts.snap @@ -1,9 +1,10 @@ -// Typelizer digest 20994e3923784dec08006faf8465bf77 +// Typelizer digest 25d43603864687a55f5a2ece2a6aa228 // // DO NOT MODIFY: This file was automatically generated by Typelizer. type AlbaMetaNil = { - id: number; + /** Unique identifier */ + id: string; } export default AlbaMetaNil; diff --git a/spec/__snapshots__/AlbaUser.ts.snap b/spec/__snapshots__/AlbaUser.ts.snap index b9b0bcc..7d53ec9 100644 --- a/spec/__snapshots__/AlbaUser.ts.snap +++ b/spec/__snapshots__/AlbaUser.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest b27b36237e51d59bd905f4931314eddd +// Typelizer digest f32f5122a1999bca2f1be94275248909 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaPost} from '@/types' type AlbaUser = { - id: number; + /** Unique identifier */ + id: string | null; username: string; active: boolean; invitor: AlbaUser; diff --git a/spec/__snapshots__/AlbaUserAuthor.ts.snap b/spec/__snapshots__/AlbaUserAuthor.ts.snap index 9e3eaf2..a6904dd 100644 --- a/spec/__snapshots__/AlbaUserAuthor.ts.snap +++ b/spec/__snapshots__/AlbaUserAuthor.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest e4ff2970ad7175d42c20d9a799d946f9 +// Typelizer digest e2058e5f2991b8733973f5969961e8d8 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaPost} from '@/types' type AlbaUserAuthor = { - id: number; + /** Unique identifier */ + id: string; /** Author login handle */ username: string | null; posts?: Array; diff --git a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap index 10cfe0f..f6a4c74 100644 --- a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 6d6055ff99fc70aef66e59e124465b4e +// Typelizer digest f40f209302edb8ff018b878f8f618eb4 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaPost} from '@/types' type AlbaUserEmptyNested = { - id: number; + /** Unique identifier */ + id: string; /** Author login handle */ username: string | null; posts?: Array; diff --git a/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap b/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap index 92c26d1..2b9cd9c 100644 --- a/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap +++ b/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 0ac7beef74fefd38c7d3ad485d8f1a73 +// Typelizer digest 8ff3ad01b621ef30a6f38a8ebc1c2436 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaUser, AlbaPost} from '@/types' type AlbaUserSerializerFoo = { - id: number; + /** Unique identifier */ + id?: number | null; username: string; active: boolean; invitor: AlbaUser; diff --git a/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap index 302840f..2be1029 100644 --- a/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap +++ b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap @@ -1,9 +1,10 @@ -// Typelizer digest 6f3a4cfc73c5faede518b793ba0f25ed +// Typelizer digest 2a963082b278d4edf62085a22140308f // // DO NOT MODIFY: This file was automatically generated by Typelizer. type AlbaVerbatimModuleSyntax = { - id: number; + /** Unique identifier */ + id: string; username: string; } diff --git a/spec/__snapshots__/AmsUser.ts.snap b/spec/__snapshots__/AmsUser.ts.snap index 0058d1e..1f7e1db 100644 --- a/spec/__snapshots__/AmsUser.ts.snap +++ b/spec/__snapshots__/AmsUser.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 5e9abced7cd48e23138f5f764cc2c608 +// Typelizer digest 84108fe00759a43c9279d9440b177111 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AmsPost} from '@/types' type AmsUser = { - id: number; + /** Unique identifier */ + id: string | null; username: string; active: boolean; invitor: AmsUser; diff --git a/spec/__snapshots__/AmsUserAuthor.ts.snap b/spec/__snapshots__/AmsUserAuthor.ts.snap index ed57d53..69e1fcd 100644 --- a/spec/__snapshots__/AmsUserAuthor.ts.snap +++ b/spec/__snapshots__/AmsUserAuthor.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 5ae23f300b01c30e04f7132b09621c12 +// Typelizer digest a1471b4ffbb57ec75c92203fb681462f // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AmsPost} from '@/types' type AmsUserAuthor = { - id: number; + /** Unique identifier */ + id: string; /** Author login handle */ username: string | null; avatar: unknown; diff --git a/spec/__snapshots__/AmsUserEmptyNested.ts.snap b/spec/__snapshots__/AmsUserEmptyNested.ts.snap index 30cec72..6b2b87f 100644 --- a/spec/__snapshots__/AmsUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AmsUserEmptyNested.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 8b51f6ff077d5703402168d15790ecb7 +// Typelizer digest dc082cf20008cc909360bca47160d30b // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AmsPost} from '@/types' type AmsUserEmptyNested = { - id: number; + /** Unique identifier */ + id: string; /** Author login handle */ username: string | null; avatar: unknown; diff --git a/spec/__snapshots__/AmsUserSerializerFoo.ts.snap b/spec/__snapshots__/AmsUserSerializerFoo.ts.snap index 6cf1da2..aa0e9ed 100644 --- a/spec/__snapshots__/AmsUserSerializerFoo.ts.snap +++ b/spec/__snapshots__/AmsUserSerializerFoo.ts.snap @@ -1,10 +1,11 @@ -// Typelizer digest 8754172bae0da06a953a1c8690e05c10 +// Typelizer digest 9fbbbdad00b20040b0a8080f124102e0 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AmsUser, AmsPost} from '@/types' type AmsUserSerializerFoo = { - id: number; + /** Unique identifier */ + id?: number | null; username: string; active: boolean; created_at: string; diff --git a/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap index 58f039b..031d5ca 100644 --- a/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap +++ b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap @@ -1,9 +1,10 @@ -// Typelizer digest 92953120c1d10142beb7678b8c7b1cd1 +// Typelizer digest ed83c27665231e5da7eddd7c9f18a293 // // DO NOT MODIFY: This file was automatically generated by Typelizer. type AmsVerbatimModuleSyntax = { - id: number; + /** Unique identifier */ + id: string; username: string; } diff --git a/spec/app/app/serializers/alba/base_serializer.rb b/spec/app/app/serializers/alba/base_serializer.rb index b47a0f7..309b8f6 100644 --- a/spec/app/app/serializers/alba/base_serializer.rb +++ b/spec/app/app/serializers/alba/base_serializer.rb @@ -4,5 +4,10 @@ class BaseSerializer helper Typelizer::DSL typelizer_config.null_strategy = :nullable_and_optional + + typelize id: [:string, comment: "Unique identifier"] + def id + Base64.urlsafe_encode64(object.id.to_s) + end end end diff --git a/spec/app/app/serializers/alba/user_serializer.rb b/spec/app/app/serializers/alba/user_serializer.rb index 8720a2d..8a6df7d 100644 --- a/spec/app/app/serializers/alba/user_serializer.rb +++ b/spec/app/app/serializers/alba/user_serializer.rb @@ -8,9 +8,13 @@ class UserSerializer < BaseSerializer has_many :posts, resource: PostSerializer has_one :latest_post, resource: PostSerializer # Duplicated association + typelize id: [:string, nullable: true] + class FooSerializer < UserSerializer typelize_from ::User attributes :created_at + + typelize id: [:number, optional: true] end end end diff --git a/spec/app/app/serializers/ams/base_serializer.rb b/spec/app/app/serializers/ams/base_serializer.rb index 7bed584..a6e0bea 100644 --- a/spec/app/app/serializers/ams/base_serializer.rb +++ b/spec/app/app/serializers/ams/base_serializer.rb @@ -3,5 +3,10 @@ class BaseSerializer < ActiveModel::Serializer include Typelizer::DSL typelizer_config.null_strategy = :nullable_and_optional + + typelize id: [:string, comment: "Unique identifier"] + def id + Base64.urlsafe_encode64(object.id.to_s) + end end end diff --git a/spec/app/app/serializers/ams/user_serializer.rb b/spec/app/app/serializers/ams/user_serializer.rb index 68d78ab..5e45459 100644 --- a/spec/app/app/serializers/ams/user_serializer.rb +++ b/spec/app/app/serializers/ams/user_serializer.rb @@ -7,9 +7,13 @@ class UserSerializer < BaseSerializer has_many :posts, serializer: PostSerializer + typelize id: [:string, nullable: true] + class FooSerializer < UserSerializer typelize_from ::User attributes :created_at + + typelize id: [:number, optional: true] end end end