Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typelizer options merging in inherited serializers #27

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/typelizer/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaInline.ts.snap
Original file line number Diff line number Diff line change
@@ -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<{
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaMetaNil.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaUser.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -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<AlbaPost>;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -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<AlbaPost>;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AmsUser.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AmsUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AmsUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AmsUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 3 additions & 2 deletions spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions spec/app/app/serializers/alba/base_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/app/app/serializers/alba/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/app/app/serializers/ams/base_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/app/app/serializers/ams/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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