ethanresnick / json-api

Turn your node app into a JSON API server (http://jsonapi.org/)
GNU Lesser General Public License v3.0
268 stars 41 forks source link

Cоmplete fix #109 issue #116

Closed bigslycat closed 7 years ago

bigslycat commented 8 years ago

schemaType.options.type[0].ref is only present in the case of relationship with another model, and is absent in the other cases (single relationship, array of symple types or subdocs). In the case when the field is a sub-document, relationship with another model or an array of sub-documents or relationships, we do not need to touch any schemaType.options.type[0].ref, or schemaType.options.type[0].name:

var typeOptions = holdsArray ?
  schemaType.options.type[0] :
  schemaType.options.type;

var isEmbeddedDocument = (
  schemaType.constructor.name === "SchemaType" ||
  schemaType.constructor.name === "DocumentArray"
);

var baseType = isEmbeddedDocument ?
  "EmbeddedDocument":
  typeOptions.name;

However, if the field will be an array of relationships, the typeOptions.name will be equal to undefined. We should not really worry about it, since under this condition to baseType is no longer appeals. But this implicit behavior, which in the further development can lead to errors. That it could not happen, we need to explicitly specify the behavior for this case.

ethanresnick commented 8 years ago

Hi @figliohulio.

Thanks so much for submitting this!

Can you please add test cases for the situations this fixes? That way, we'll make sure not to break those situations if we need to tweak this code again in the future (since, as you can see, the Mongoose schema code is quite finicky).