JSON Type provider with Saturn Framework `Controller.getModel<MyModel>` not working

135 Views Asked by At

I've some trouble getting my type to work with Saturn and the JSON type-provider.

My type: (generated by the typeprovider)

[<CLIMutable>]
type FeatureModel = JsonProvider<"../example.json">

My code in the Controller.create Action:

...
let! inputModel = Controller.getModel<FeatureModel> ctx
let uploadedName = inputModel.Feature.Name //Example
...

I would expect intellisense to work for inputModel.Feature.Name, but it doesn't. I've validated the type. If I use it directly with FeatureModel.GetSample() it does show me properties/fields.

Any idea what I'm missing or doing wrong?

1

There are 1 best solutions below

0
On

you can't use CLIMUtable on a type provider, a that's an erased type (unfortunately).

[<CLIMutable>]
type FeatureModel = JsonProvider<"../example.json">

the above line shouldn't compile i think.

The resons is some type providers (Erased types) are erased at compile time, so they cannot be used by CLI (common language interface and runtime), but you might have other ways to use those in controllers.

Maybe using some other custom serializers or Saturn, instead of using aspnectore controller binding directly, or creating your own custom binding.

else you could pass a raw string in the controller, and use FeatureModel.Parse inside of it (probably that works), as suggested above