contentful / contentful_model

A lightweight wrapper around the Contentful api gem, to make it behave more like ActiveRecord
MIT License
44 stars 42 forks source link

Creating objects via Management API with many-references not working #36

Closed toadle closed 8 years ago

toadle commented 8 years ago

Hey guys,

we are trying to populate an initial content-set via contentful_model. Now we have classes like this:

class ContentfulPerson < ContentfulModel::Base
  self.content_type_id = 'person'

  has_one :organization, class_name: 'ContentfulOrganization'
  has_many :abilities, class_name: 'ContentfulAbility'
end

Now creating a person like so ContentfulPerson.create(name: 'some_name') is working. But what is now working is something like this:

p = ContentfulPerson.create(name: 'some_name')
o = ContentfulOrganization.find_by(...).load.first
p.organization = o
p.save

Gives me

SystemStackError: stack level too deep
from /Users/tim/.rbenv/versions/2.2.3/gemsets/kplus-pipedrive-export/gems/activesupport-5.0.0/lib/active_support/core_ext/object/instance_variables.rb:13:in `map'

But I also tried

t = ContentfulAbility.find_by(...).load.first
p.abilities = [t]
p.save
=> #<Contentful::Management::UnprocessableEntity: Validation error>

Is this supposed to work already?

Sidenote: I did this successfully with just using classes and objects from contentful.rb and contentful-management.rb. In combination they support setting references like this.

Thanks for any help!

toadle commented 8 years ago

I investigated this further. The Validation-problem that occurs is this one:

"{
  "sys": {
    "type": "Error",
    "id": "InvalidEntry"
  },
  "message": "Validation error",
  "details": {
    "errors": [
      {
        "name": "type",
        "value": null,
        "type": "Object",
        "details": "The type of \\"value\\" is incorrect, expected type: Object",
        "path": [
          "fields",
          "abilities",
          "en-US",
          0
        ]
      },
      {
        "name": "type",
        "value": null,
        "type": "Object",
        "details": "The type of \\"value\\" is incorrect, expected type: Object",
        "path": [
          "fields",
          "abilities",
          "en-US",
          1
        ]
      },
      {
        "name": "type",
        "value": null,
        "type": "Object",
        "details": "The type of \\"value\\" is incorrect, expected type: Object",
        "path": [
          "fields",
          "abilities",
          "en-US",
          2
        ]
      },
      {
        "name": "type",
        "value": null,
        "type": "Object",
        "details": "The type of \\"value\\" is incorrect, expected type: Object",
        "path": [
          "fields",
          "abilities",
          "en-US",
          3
        ]
      }
    ]
  },
  "requestId": "..."
}

Seems like the API does not accept the creation of Links via the API in the format that is currently send. How is it supposed to be send? There isn't any documentation online.

dlitvakb commented 8 years ago

Hey @toadle,

Looks like I totally overlooked this case, I forgot linked entries somehow 😅

I might be able to have a simple fix early next week.

Would that be ok for you?

Cheers

toadle commented 8 years ago

@dlitvakb Thanks for getting back! A quick fix would be really great.

Also I have to revise my previous statement. I tried to replicate this with just using contentful.rb and content-management.rb.

Some time ago doing this:

space = Contentful::Management::Space.find(...)
content_type = space.content_types.find(...)
content_type.entries.create(@fields[default_locale])

in a Contentful::Entry worked. I added other Contentful::Entrys as properties there.

Sadly now it does not anymore.

Did the API change?

toadle commented 8 years ago

No, it did not. #selfanswer :-)

@dlitvakb But should be fixed by this: https://github.com/contentful/contentful-management.rb/pull/105

Only other solution I see is to first convert everything from Contentful::Entry to Contentful::Management::Entry before saving. Root cause was that contentful-management.rb would convert an [<Contentful::Entry>, <Contentful::Entry>, <Contentful::Entry>] to [nil,nil,nil] which is not OK with the API.

dlitvakb commented 8 years ago

Hey @toadle,

Sounds good! Just made a few comments on that PR.

Cheers