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

Nested Serializers #19

Open
mkdynamic opened this issue Jan 23, 2024 · 3 comments
Open

Nested Serializers #19

mkdynamic opened this issue Jan 23, 2024 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@mkdynamic
Copy link

mkdynamic commented Jan 23, 2024

Howdy! Given this serializer (in a single file, verbatim):

class FooSerializer < BaseSerializer
  class BarSerializer < BaseSerializer
    attributes :id
  end

  belongs_to :bar, serializer: BarSerializer
end

The generated TS file is invalid:

import type FooSerializerBar from './FooSerializer/Bar'

export default interface Foo {
  bar: FooSerializerBar
}

The ./FooSerializer/Bar does not exist.

Am I doing something wrong, or is this currently unsupported?

I find this pattern of nesting serializers is very useful when you need a representation of a child resource that depends on the parent context vs. having one large serializer in the base directory with a lot of flags/conditionals.

@ElMassimo
Copy link
Owner

Hi Mark! Thanks for reporting!

This is indeed a bug, ideally nested serializers would be included in the same file.

this pattern of nesting serializers is very useful

Completely agree.

@ElMassimo ElMassimo added bug Something isn't working enhancement New feature or request labels Jan 23, 2024
@ElMassimo
Copy link
Owner

Didn't realize until now, but a workaround I was using in my projects is to have it in the same file but with a different name:

class FooBarSerializer < BaseSerializer
  attributes :id
end

class FooSerializer < BaseSerializer
  belongs_to :bar, serializer: FooBarSerializer
end

However, the nested idiom is better because it avoids polluting the global namespace.

@rolftimmermans
Copy link

The inline serializers are explicitly removed from the list of serializers to generate interfaces for. Removing this check seems to work? What was the original reason to skip generating types for inline serializers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants