ユーザーを公開するWebAPIがあります。
各ユーザーには、ユーザーでもあるマネージャーがいます。
私のEDMXには、「user」エンティティに自己1 .. *ナビゲーションプロパティがあります。これは、マネージャーからユーザーへのナビゲーション用の「コラボレーター」であり、ユーザーからマネージャーへのナビゲーション用の「マネージャー」です。
私のJSONAPIは、NewtonJSONを使用してエンティティをシリアル化します。
API呼び出しのJSON結果をカスタマイズするために、クエリ文字列にキーワード「fields」を実装しました。「フィールド」を使用すると、ユーザーの部分的なJSON表現を取得できるようになります。
呼び出しは次のようになります:/ api / users?fields = id、name、department、picture
そして、完全なC#ユーザーオブジェクトから、id、name、department、およびpictureプロパティのみを含むJSONを取得します。
IContractResolverのカスタム実装を使用してこれを達成しました。
問題は、NewtonJSONコントラクトリゾルバーが「オブジェクトごと」ではなく「タイプごと」に機能することです。つまり、宣言型のそのようなメンバーを別のメンバーではなくシリアル化するようにシリアライザーに指示できますが、それを指示することはできません(私の知る限り、それが私がここで尋ねる理由です)同じタイプのこのオブジェクトのそのようなメンバーをシリアル化し、同じタイプの別のオブジェクトの同じメンバーではありません。
そうは言っても、私の問題は私が尋ねるときです:/ api / users?fields = id、name、manager
次のように、各ユーザーオブジェクトのマネージャーメンバーの再帰的なシリアル化で応答を受け取ります。
[{
id: 123,
name: "Foo",
manager:
{
id: 124,
name: "Foo",
manager:
{
id: 125,
name: "Foo",
manager:
{
...
}
}
}
},
{
id: 124,
name: "Foo",
manager:
{
id: 125,
name: "Foo",
manager:
{
...
}
}
},
{
id: 125,
name: "Foo",
manager:
{
...
}
}]
次のように、部分的なサブエンティティの応答を要求する機能も実装しました。
/api/users?fields=id,name,manager.id
ただし、メインオブジェクト(ここではユーザー)とサブオブジェクト(マネージャー)が両方とも同じタイプであるため、機能していません。
部分応答WebAPIを実装するためにNewtonJSONをすでに使用している人はいますか?埋め込まれた自己タイプエンティティをどのように操作しますか?
アドバイスありがとうございます。