question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[JS][Authoring] Custom fields based on `Image` not being serialised

See original GitHub issue

Target Application

Twist.com (our own renderer)

Problem Description

I have created a custom component that extends the Image class, the class itself is fairly simple, it’s literally just:

export class Imageist extends Image {
    static readonly JsonTypeName = 'Image'
    static readonly aspectRatioProperty = new NumProperty(Versions.v1_0, 'aspectRatio')

    @property(Imageist.aspectRatioProperty)
    aspectRatio?: number

    getJsonTypeName(): string {
        return Imageist.JsonTypeName
    }
}

When I try and use this new field in my application, it doesn’t get serialised. Calling getSchema() on it doesn’t show the new field at all and I’m unsure why.

I have created a few custom components with additional fields in exactly the same way and all of them have worked without an issue. An example is one based on TextInput

export class TextInputist extends TextInput {
    static readonly JsonTypeName = 'Input.Text'
    static readonly rowsProperty = new NumProperty(Versions.v1_0, 'rows')
    static readonly inputStyleProperty = new StringProperty(Versions.v1_0, 'inputStyle')

    @property(TextInputist.rowsProperty)
    rows?: number

    @property(TextInputist.inputStyleProperty)
    inputStyle?: InputStyle

    getJsonTypeName(): string {
        return TextInputist.JsonTypeName
    }
}

The new fields here (rows and inputStyle) get serialised just fine without having to do anything except use them. I’ve actually created custom versions of AdaptiveCard, TextInput, and ChoiceSetInput all with additional fields, and none of them have had an issue when it comes to being serialised.

I wonder whether it has anything to do with the fact none of the others I’ve done had a base class that overrode the populateSchema function? I actually did try this, and added my own override that explictly adds the aspectRatioProperty to the schema, but it still gets ignored.

Expected Outcome

I was expecting the new fields to be serialised in the same way as other new components I’ve written.

Actual Outcome

The new fields don’t appear in the schema and so aren’t serialised.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dclauxcommented, Aug 20, 2021

@scottlovegrove technically, you shouldn’t have to override getSchemaKey, that’s why I didn’t document it explicitly. By default, getSchemaKey returns the same value as getJsonTypeName. But the assumption is that most of the time, any given JSON type name will be used by a single class. Your (perfectly valid) implementation of Imageist uses the same JSON type name as the built-in Image, and so it has the same schema key, which is why in that “special” case you need to override getSchemaKey.

0reactions
scottlovegrovecommented, Aug 20, 2021

THanks @dclaux that fixed it 👍🏻 Is that something I missed somewhere in the docs? Looking at this I can’t see any mention of that function. This is basically where I started with our new components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image added in custom fields only returning image ID in ...
I went to custom fields and double checked that the return was set to “image url”, not array or media ID. but it's...
Read more >
Django REST - rest_auth custom fields not being serialized
I'm using Django REST with the rest_auth package, and I'm having an issue with serializing custom fields when registering a new user.
Read more >
Custom field for image not being pulled in
Hello,. Working with the dynamic elements in GP and Blocks and have a custom field for a photo that is not being pulled...
Read more >
Add custom fields to image field - Drupal Answers
I have a case where I need to add three custom fields to each image in an image field to hold data. The...
Read more >
Attach images to posts using custom fields (just paths ...
I does that already, the problem is that there is no attached files the CPT. I'm trying to figure out how to attach...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found