Allow explicit keys for entities in API results.

This adds parameters for explicitly giving a key when adding an entity
to an API result. This is needed to handle redirects correctly, where
the requested ID, which should be used as the key in the result, is not
the actual ID of the entity.

This also introduces a more compelx fallback logic for determining the
key used for missing entities.

Bug: 45509
Change-Id: Ica78bc830fed8bc61b797a0c6f9ec3bf21ecb370
Merge "Allow explicit keys for entities in API results."