Custom model member
Last updated
Last updated
With the custom data type on a model member you can specify external (from the perspective of the REST project tool) data types. You use Custom specification to specify the type.
Currently the only available custom data types are types originating from the .NET Base Class Library (BCL). To use a type from BCL you prefix the full name of that type with 'bcl:', for instance 'bcl:System.TimeSpan'. Note that certainly not all types will work, and most of them will make no sense. You can consider this feature as an advanced one with limited support available. It is however powerful and useful in many scenarios and can save you a lot of development time and need of custom connectors by using it properly. Since it can reduce the need for custom connectors it also simplifies deployment and reduces maintenance.
So, after this warning, let's walk through an example of how it can be used.
Consider this scenario:
You want to fetch pdf documents from a REST API
The REST API returns the pdf documents as base64 encoded strings
You want to store these documents in the filesystem, of course not as base 64 encoded string but as a pdf file.
This is how the json looks like when returned from the server:
Since this is what the API will return, we need a model for it. This is quickly done with 'NEW FROM JSON' in the Model part of the REST Project Tool. See Models for more information on how to to that.
Once we've created the model it will look like this:
We now need a model that contains not the base 64 encoded string, but instead an instance of System.IO.Stream. Since this model will look almost the same as the 'Invoice' model, we can use the 'Clone model' feature. This is accessed by selecting the tree node for the model and right clicking:
We'll call this second model 'Invoice (binary)'. We now have two identical models:
Next step is to change the data type of 'Data' in 'Invoice (binary)'. Select the 'Data' member and change 'Type' to 'Custom' and set 'Custom specification' to 'bcl:System.IO.Stream'.
Next step is to create a model transformation between the two models.
The code could look something like this:
Now this transformation can be applied to a GET operation
Finally the workflow could look something like this: