Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Image ids are incorrect in API output #470

Closed
chrismytton opened this issue May 15, 2014 · 5 comments · Fixed by mysociety/popit-api#136
Closed

Image ids are incorrect in API output #470

chrismytton opened this issue May 15, 2014 · 5 comments · Fixed by mysociety/popit-api#136

Comments

@chrismytton
Copy link
Member

e.g. on http://congresistas-gt.popit.mysociety.org/api/v0.1/persons/5367ae9d7ee559063fbc6b6e the object in the images array has the following id property:

"_id": {
  "id": "Sr.~ö5?Bed\u0007ú",
  "_bsontype": "ObjectID"
}

Where it should actually just be a string:

"_id": "53722e7ef6353f42656407fa"

Additionally it would be useful if each object in the images array had a url property pointing to the image.

@chrismytton
Copy link
Member Author

I've just been looking at this and it seems that when the person record is re-saved the image id gets correctly saved as a string. So there's something funny going on when the image is first added to the record.

@struan
Copy link
Member

struan commented Sep 10, 2014

Looking at this the problem is as much that popit is storing an images array which is not in line with the popolo spec. Ideally we should be returning just an image property which points to either the external URL or the URL of the image we have stored.

@chrismytton
Copy link
Member Author

Fixed in mysociety/popit-api@b223d28

@chrismytton
Copy link
Member Author

Still seeing this issue when uploading a new image to a newly created document.

@chrismytton
Copy link
Member Author

The issue here is that the image is added to the images array by the UI. In the UI the Person schema has an embedded Image schema, so when an image is added to the images array it gets an _id property which is a mongo ObjectId. However, when the API then gets the requested person out of the database it doesn't have an embedded image schema, it just sees images as an array of plain object, in this case it seems that mongoose doesn't do the conversion to a string representation of an ObjectId, which is why we we see the strange _bsontype property.

So tl;dr the root of this issue is the fact that we have two sets of schemas accessing the same database table (one in the UI, on in the API) but representing them in different ways. The fix therefore is to consolidate the schemas so there is One True Source of data modelling.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants