Issue with RangeFilters Metadata Propagation and TypedJson Serialization (Angular)

Discussão em 'Angular' iniciado por FLG 99, Março 18, 2025.

  1. FLG 99

    FLG 99 Guest

    I am facing an issue with metadata propagation in my RangeFilters object. I need to copy metadata only to specific instances rather than applying it globally. However, my current approach affects all instances, which causes unwanted behavior.

    Additionally, when using TypedJson with jsonMember(String), metadata does not get applied correctly.

    Here is an updated example illustrating the problem using a different class:

    class DateRangeFilters {
    dateFrom?: Date;
    dateTo?: Date;
    }

    function PropagateDateFilterMetadata() {
    return function (target: any, propertyKey: string) {
    propagateMetadata(target, propertyKey, DateRangeFilters, "dateFrom");
    propagateMetadata(target, propertyKey, DateRangeFilters, "dateTo");
    };
    }

    function propagateMetadata(sourceObject: Object, sourceProperty: string, targetClass: any, targetProperty: string) {
    const metadataKeys = Reflect.getMetadataKeys(sourceObject.constructor.prototype, sourceProperty);

    metadataKeys.forEach((key) => {
    const metadataValue = Reflect.getMetadata(key, sourceObject.constructor.prototype, sourceProperty);
    Reflect.defineMetadata(key, metadataValue, targetClass.prototype, targetProperty);
    });
    }

    // Example usage
    class Event {
    @PropagateDateFilterMetadata()
    @DateFormat("yyyyMMdd")
    @jsonMember(String)
    eventDate: DateRangeFilters;
    }

    Metadata Propagation Issue


    I want metadata to be copied only to specific instances of RangeFilters, but it is applied globally to all instances. Is there a way to scope metadata to a particular instance instead of modifying the prototype?

    TypedJson Serialization Issue


    When using TypedJson with jsonMember(String), metadata is not preserved as expected. Could there be a conflict between Reflect.defineMetadata and TypedJson? How can I ensure metadata is properly copied only to specific instances and make TypedJson correctly handle metadata? Any help would be appreciated!

    Continue reading...

Compartilhe esta Página