Adding additional data to meta layer

I am using /metadata/app.json directly to scaffold my front end.

In some places I need to use the [Meta] attribute to add in additional context. Sometimes several values are needed on a single property so I have to do something like this:

[Meta("Lookup", "Store:Reseller,Display:Search")]
public int? ResellerId { get; set; }

This is a bit fragile and I’d like to have a wrapper of some sort so I can strongly type it or a way to serialize a custom settings object into the app.json request property object.

I tried making an attribute that inherits MetaAttribute but it didn’t work as I guess calling code looks for the MetaAttribute type specifically.

May I ask for some advice on what would be the best way to add additional data to app.json from a property decorator please?

If you’re talking about /metadata/app.json that’s primarily a concrete typed data structure, the only place to attach custom metadata is going to be in the Meta string dictionary, so either way it’s going to be stringly typed.

But if needed you can modify the returned app.json by modifying the data structure in the AddToAppMetadata extension method, e.g:

appHost.AddToAppMetadata(metadata =>
{
    metadata.Api.Operations.Each(op =>
    {
        op.Request.Meta ??= new();
        op.Request.Meta["MyMeta"] = "...";
    });
});

If you want it more typed you could use some constants instead, e.g:

static class MetaType
{
    public const string Lookup = nameof(Lookup);
}

[Meta(MetaType.Lookup,..)]

Otherwise you’re probably best to forget augmenting app.json and just return your own custom metadata that looks at your own custom attributes. If needed you can iterate your App’s operations with:

foreach (var op in HostContext.Metadata.Operations)
{
    var requestDto = op.RequestType;
    var attr = requestDto.FirstAttribute<MyAttr>();
}

Thanks for the detailed reply Mythz.

Is there anyway to add more than 1 entry to the meta string dictionary in the model definition? I t looks like only 1 meta attribute is allowed per property. If its a string dictionary should AllowMultiple = true?

AllowMultiple is now added to [Meta] available from v8.5.3+ that’s now available in pre-release packages.

1 Like

Awesome! Thanks Mythz, that will make things a bit easier for me.

1 Like