active_fedora icon indicating copy to clipboard operation
active_fedora copied to clipboard

Can't set AF::Base as the object of a predicate

Open awead opened this issue 9 years ago • 5 comments

This demonstrated here: https://github.com/projecthydra/active_fedora/tree/uri-metadata

Given a simple model:

    class BookTitle < ActiveFedora::Base
      property :label, predicate: ::RDF::Vocab::SKOS.prefLabel
    end

    class Book < ActiveFedora::Base
      property :title, predicate: ::RDF::Vocab::DC.title, class_name: "BookTitle"
    end

You can't do something like:

uri_title = BookTitle.create(label: ["A title"])
Book.create(title: [uri_title])

Without getting something like: Ldp::HttpError: STATUS: 403 http://localhost:52444/rest/test/99/e6/36/59/99e63659-59d9-47a8-b928-3ee35429e010 is not in the topic of this RDF, which is http://localhost:52444/rest/test/45/10/d2/93/4510d293-f4bf-44b2-aaf4-de04f23c9d72...

awead avatar Sep 08 '16 03:09 awead

I'm guessing that ActiveTriples doesn't know how to cast ActiveFedora::Base objects to URIs.

jcoyne avatar Sep 08 '16 03:09 jcoyne

Here's the full stack trace:

  1) URIs as objects with a book's title sets the title to the uri
     Failure/Error: super

     Ldp::HttpError:
       STATUS: 403 http://localhost:8986/rest/test/4c/f4/46/ce/4cf446ce-44fe-4e05-9bbb-279b55d13ee3 is not in the topic of this RDF, which is http://localhost:8986/rest/test/f5/c6/43/38/f5c64338-b056-4190-a31c-28195cc019f5...
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/client/methods.rb:119:in `block in check_for_errors'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/client/methods.rb:117:in `tap'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/client/methods.rb:117:in `check_for_errors'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/client/methods.rb:82:in `block in post'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/notifications.rb:166:in `instrument'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/client/methods.rb:74:in `post'
     # ./lib/active_fedora/caching_connection.rb:19:in `post'
     # ./lib/active_fedora/initializing_connection.rb:37:in `post'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/resource.rb:83:in `create'
     # /Users/awead/.gem/ruby/2.3.1/gems/ldp-0.6.1/lib/ldp/resource/rdf_source.rb:20:in `create'
     # ./lib/active_fedora/persistence.rb:167:in `_create_record'
     # ./lib/active_fedora/indexing.rb:63:in `_create_record'
     # ./lib/active_fedora/callbacks.rb:240:in `block in _create_record'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:778:in `_run_create_callbacks'
     # ./lib/active_fedora/callbacks.rb:240:in `_create_record'
     # ./lib/active_fedora/persistence.rb:159:in `create_or_update'
     # ./lib/active_fedora/callbacks.rb:236:in `block in create_or_update'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:117:in `call'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:505:in `call'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
     # /Users/awead/.gem/ruby/2.3.1/gems/activesupport-4.2.7/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
     # ./lib/active_fedora/callbacks.rb:236:in `create_or_update'
     # ./lib/active_fedora/persistence.rb:29:in `save'
     # ./lib/active_fedora/validations.rb:50:in `save'
     # ./spec/integration/with_uri_metadata_spec.rb:25:in `block (3 levels) in <top (required)>'

awead avatar Sep 08 '16 12:09 awead

I wonder if it's attempting to persist more than one resource in a single graph and Fedora just doesn't like that?

jcoyne avatar Sep 08 '16 13:09 jcoyne

Yes, I think you're right. If I look at ldp-0.6.1/lib/ldp/resource.rb:83, the first call is to created the title resource, and that looks fine:

<> <http://www.w3.org/2004/02/skos/core#prefLabel> "My Book Title";
   <info:fedora/fedora-system:def/model#hasModel> "BookTitle" .

The next call is all wrong though. When it posts the triples to create the Book resource, there are multiple statements:

<> <http://purl.org/dc/terms/title> <http://localhost:8986/rest/test/6e/a2/4b/7c/6ea24b7c-dbed-482d-85df-56b750e7f28d>;
   <info:fedora/fedora-system:def/model#hasModel> "Book" .

<http://localhost:8986/rest/test/6e/a2/4b/7c/6ea24b7c-dbed-482d-85df-56b750e7f28d> a <http://fedora.info/definitions/v4/repository#Resource>,
     <http://fedora.info/definitions/v4/repository#Container>,
     <http://www.w3.org/ns/ldp#RDFSource>,
     <http://www.w3.org/ns/ldp#Container>;
   <http://fedora.info/definitions/v4/repository#created> "2016-09-08T13:24:18.795Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
   <http://fedora.info/definitions/v4/repository#createdBy> "bypassAdmin";
   <http://fedora.info/definitions/v4/repository#exportsAs> <http://localhost:8986/rest/test/6e/a2/4b/7c/6ea24b7c-dbed-482d-85df-56b750e7f28d/fcr:export?format=jcr/xml>;
   <http://fedora.info/definitions/v4/repository#hasParent> <http://localhost:8986/rest/test>;
   <http://fedora.info/definitions/v4/repository#lastModified> "2016-09-08T13:24:18.795Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>;
   <http://fedora.info/definitions/v4/repository#lastModifiedBy> "bypassAdmin";
   <http://fedora.info/definitions/v4/repository#numberOfChildren> "0"^^<http://www.w3.org/2001/XMLSchema#long>;
   <http://fedora.info/definitions/v4/repository#writable> true;
   <http://www.w3.org/2004/02/skos/core#prefLabel> "My Book Title";
   <info:fedora/fedora-system:def/model#hasModel> "BookTitle" .

<http://localhost:8986/rest/test/6e/a2/4b/7c/6ea24b7c-dbed-482d-85df-56b750e7f28d/fcr:export?format=jcr/xml> <http://purl.org/dc/elements/1.1/format> <http://fedora.info/definitions/v4/repository#jcr/xml> .

The first one looks right, but the second looks all wrong. I don't know what the third is.

awead avatar Sep 08 '16 13:09 awead

@jcoyne I've isolated the problem. You're right, AT doesn't know how to cast AF::Base objects to URIs. If I pass the uri as Book.create(title: [uri_title.uri]) it works.

The issue creeps up here: https://github.com/ActiveTriples/ActiveTriples/blob/0.10-stable/lib/active_triples/relation.rb#L358 Calling BookTitle.resource returns a BookTitle::GeneratedResourceSchema object, which is a subclass of RDFResource so AT adds it as a child resource.

I'm not sure how I would solve this. Suggestions? I'm inclined to just call .uri on everything.

awead avatar Sep 09 '16 01:09 awead