janaipakos / ghibliapi

A simple API for Studio Ghibli resources. Unofficial/fan-made
https://ghibliapi.herokuapp.com
MIT License
310 stars 109 forks source link

When accessing a singular object from any endpoint, if that single item has a `.length` property, it is evaluated by Math.min against the constant `defaultLimit`. #36

Closed atlc closed 3 years ago

atlc commented 3 years ago

For example, in the /vehicles endpoint, if you retrieve all vehicles from that endpoint, it returns an array of all the vehicles. Since the data variable is defined as an array, the .length property successfully shortens the length of the array, creating a limit to the amount of results.

If you attempt to access a single resource, like /vehicles/4e09b023-f650-4747-9ab9-eacf14540cfb, that data variable is now represented as an object. Since objects don't have an inherit .length property to them, the .length property of the data is being mutated. This doesn't pose an issue anywhere else except the vehicles endpoint, where the objects do have that property.

Furthermore, if the length of the vehicle itself is shorter than the defaultLimit value, the data goes by unmodified. If a vehicle is longer than defaultLimit, it is overridden to defaultLimit. If the vehicle is longer than 999, Math.min will return NaN because the length property is initially stored as a string with commas.

My suggestion would be omitting modifying data.length altogether in the event a single resource is requested. If someone is requesting just a single item, they'll only have that single object returned.