Google Groups Home
Help | Sign in
Sharing a resource's schema through REST
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  22 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Eric Mill  
View profile
 More options Apr 20 2007, 2:03 pm
From: Eric Mill <em...@thoughtbot.com>
Date: Fri, 20 Apr 2007 18:03:10 -0000
Local: Fri, Apr 20 2007 2:03 pm
Subject: Sharing a resource's schema through REST
I propose that Rails and ActiveResource add support for retrieving a
model's schema via REST.  This could make it much easier for REST
clients to auto-figure out attributes, the way that ActiveRecord uses
the database to do the same thing.

The best location for this is, I think, at "/users/new.xml", using a
"User" model as the example.  The HTML at "/users/new" provides the
form needed to create a new instance, and the XML version would
provide the information needed to do the same thing remotely.

Here's one idea for a clean syntax for doing this (also at
http://pastie.caboo.se/55138):

respond_to do |format|
  format.xml { render :xml => User.to_xml }
end

This would mean writing a to_xml function on ActiveRecord::Base as a
class method.  The XML this produces could look like the normal AR
to_xml format, just without any content.  An example:

http://pastie.caboo.se/55136

I've been working on an ActiveResource client in JavaScript, and it's
impossible to just assign properties to an object and for the object
to know that these are attributes to be submitted along with any POST
or PUT call.  ActiveResource has a partial solution to this by taking
advantage of Ruby's method_missing, so any unknown properties that get
assigned can be caught and recognized as formal attributes.  Clients
written in strongly typed languages would have an even harder time
than JavaScript.  Including an easy way to advertise the schema would
ease the burden of any REST client.

More generally, I think this makes the Rails REST standard more
complete, by affording an ActiveResource client the same level of
schema knowledge that an ActiveRecord server has.

What do you guys think?  Would this be a useful extension to the
current REST standard Rails is advocating?  I think the most
interesting part of this is the XML format, and I'd really like to
form something the community agrees with as I construct a patch for
this.

-- Eric Mill


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr. Ernie Prabhakar  
View profile
 More options Apr 20 2007, 2:18 pm
From: "Dr. Ernie Prabhakar" <ernest.prabha...@gmail.com>
Date: Fri, 20 Apr 2007 11:18:42 -0700
Local: Fri, Apr 20 2007 2:18 pm
Subject: Re: [Rails-core] Sharing a resource's schema through REST
Hi Eric,

On Apr 20, 2007, at 11:03 AM, Eric Mill wrote:

> What do you guys think?  Would this be a useful extension to the
> current REST standard Rails is advocating?  I think the most
> interesting part of this is the XML format, and I'd really like to
> form something the community agrees with as I construct a patch for
> this.

I like!  I do think a little more metadata would make it easier to  
build loosely coupled systems around REST and Rails.

-- Ernie P.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Koziarski  
View profile
 More options Apr 20 2007, 5:55 pm
From: "Michael Koziarski" <mich...@koziarski.com>
Date: Sat, 21 Apr 2007 09:55:17 +1200
Local: Fri, Apr 20 2007 5:55 pm
Subject: Re: [Rails-core] Sharing a resource's schema through REST

> respond_to do |format|
>   format.xml { render :xml => User.to_xml }
> end

> This would mean writing a to_xml function on ActiveRecord::Base as a
> class method.  The XML this produces could look like the normal AR
> to_xml format, just without any content.  An example:

> http://pastie.caboo.se/55136

Why not generate an xml schema?  or RelaxNG?  Inventing a schema
language seems like the wrong direction...

> More generally, I think this makes the Rails REST standard more
> complete, by affording an ActiveResource client the same level of
> schema knowledge that an ActiveRecord server has.

--
Cheers

Koz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sandofsky  
View profile
 More options Apr 20 2007, 7:43 pm
From: Sandofsky <sandof...@gmail.com>
Date: Fri, 20 Apr 2007 23:43:59 -0000
Local: Fri, Apr 20 2007 7:43 pm
Subject: Re: Sharing a resource's schema through REST
What would a RelaxNG schema look like for the example? Here's one I
found:

http://relaxng.org/tutorial-20011203.html#IDAHDYR

And that looks more complicated than it needs to be.

Ben

On Apr 20, 2:55 pm, "Michael Koziarski" <mich...@koziarski.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Koziarski  
View profile
 More options Apr 20 2007, 8:29 pm
From: "Michael Koziarski" <mich...@koziarski.com>
Date: Sat, 21 Apr 2007 12:29:30 +1200
Local: Fri, Apr 20 2007 8:29 pm
Subject: Re: [Rails-core] Re: Sharing a resource's schema through REST

> And that looks more complicated than it needs to be.

Depends  on what your requirements are.   If you want a 'schema
language' to help non-rails clients, why not use one that's been
widely tested and has support in many different languages.  Making up
something specific for rails would mean it's of questionable value to
non-rails programming environments.

I'm not necessarily sold on the idea of generating a particular schema
language, or even of generating a schema at all,  but I do know that
inventing our own is something we should be very wary of.

--
Cheers

Koz


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Munat  
View profile
 More options Apr 20 2007, 8:35 pm
From: Ben Munat <bmu...@gmail.com>
Date: Fri, 20 Apr 2007 14:35:08 -1000
Local: Fri, Apr 20 2007 8:35 pm
Subject: Re: [Rails-core] Re: Sharing a resource's schema through REST
+1


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Kubb  
View profile
 More options Apr 20 2007, 10:54 pm
From: Dan Kubb <dan.k...@autopilotmarketing.com>
Date: Fri, 20 Apr 2007 19:54:27 -0700
Local: Fri, Apr 20 2007 10:54 pm
Subject: Re: [Rails-core] Sharing a resource's schema through REST
Hi Eric,

> I propose that Rails and ActiveResource add support for retrieving a
> model's schema via REST.  This could make it much easier for REST
> clients to auto-figure out attributes, the way that ActiveRecord uses
> the database to do the same thing.

+1, I think this is a great idea.

Since we're talking about this, I had the idea of using the OPTIONS
method to return the methods allowed on a resource (via the Allow
header), but also returning an XML representation of the routes for
the nested and associated resources.

The OPTIONS response is supposed to return a comma separated list
of methods allowed in the Allow header, but the body can really be
anything we want since the spec doesn't say what it should be.  If
people think this is a good fit, we could use it as a way of
"introspecting" a resource.

Combine these two ideas, and we have a way for a remote client to
create new resources AND a way of finding all the associated resources.

--

Thanks,

Dan
__________________________________________________________________

Dan Kubb
Autopilot Marketing Inc.

Email: dan.k...@autopilotmarketing.com
Phone: 1 (604) 820-0212
Web:   http://autopilotmarketing.com/
__________________________________________________________________


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Pope  
View profile
 More options Apr 21 2007, 12:20 am
From: Tim Pope <rubyonrails-c...@tpope.info>
Date: Fri, 20 Apr 2007 23:20:33 -0500
Local: Sat, Apr 21 2007 12:20 am
Subject: Re: Sharing a resource's schema through REST

On Sat, Apr 21, 2007 at 12:29:30PM +1200, Michael Koziarski wrote:
> Depends  on what your requirements are.   If you want a 'schema
> language' to help non-rails clients, why not use one that's been
> widely tested and has support in many different languages.  Making up
> something specific for rails would mean it's of questionable value to
> non-rails programming environments.

Along these lines, I've been investigating generating a W3C XML Schema
from a model and found it delightfully easy with just a bit of
reflection on AR::Base::columns.  Throw in a .xsd format and you've
got a great place to serve your schema.

One of the things I noticed was how similar the standard types for an
XML Schema were to the types used by #to_xml.  With just a few very
slight changes, Rails can be using a standardized list of types
instead of inventing its own.

I've refactored part of Hash::from_xml and in the process, added
support for the parsing end of things for these types.  This also
fixes a few issues as noted in the ticket I submitted it with.  It
goes without saying tests are included.  If there's interest, I can
fix up the generating part (the various #to_xml's) as well.

http://dev.rubyonrails.org/ticket/8047

> I'm not necessarily sold on the idea of generating a particular schema
> language, or even of generating a schema at all,  but I do know that
> inventing our own is something we should be very wary of.

I agree, most of this is destined for plugin land at most.  But I
think reusing the types in the core is a Good Thing.

Cheers,
Tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr. Ernie Prabhakar  
View profile
 More options Apr 21 2007, 7:23 pm
From: "Dr. Ernie Prabhakar" <ernest.prabha...@gmail.com>
Date: Sat, 21 Apr 2007 16:23:03 -0700
Local: Sat, Apr 21 2007 7:23 pm
Subject: Re: [Rails-core] Re: Sharing a resource's schema through REST
Hi Tim,

On Apr 20, 2007, at 9:20 PM, Tim Pope wrote:

> If there's interest, I can
> fix up the generating part (the various #to_xml's) as well.

Wow, great work. I'd certainly be interested in seeing that...

-enp


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Pope  
View profile
 More options Apr 22 2007, 12:33 am
From: Tim Pope <rubyonrails-c...@tpope.info>
Date: Sat, 21 Apr 2007 23:33:35 -0500
Local: Sun, Apr 22 2007 12:33 am
Subject: Re: [Rails-core] Re: Sharing a resource's schema through REST

On Sat, Apr 21, 2007 at 04:23:03PM -0700, Dr. Ernie Prabhakar wrote:
> > If there's interest, I can
> > fix up the generating part (the various #to_xml's) as well.

> Wow, great work. I'd certainly be interested in seeing that...

Okay.  I see that my first patch was committed this morning (thanks
DHH) so this is the next logical step.  Note that by itself, this
change enables nothing without a proper schema definition.  Basically
it would be trading a little bit of aesthetics (e.g., datetime for
dateTime) for a little bit of purity (a "standard" type list).

On the subject of XML schemas, the biggest missing piece of the puzzle
is a way to get the appropriate attributes on the root element for
records.  I am thinking an interface would look something like

@people.to_xml(
  :root_attributes => {
    'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    'xsi:noNamespaceSchemaLocation' => formatted_people_url(:xsd)
  }
)

The implementation should be simple enough, but before I do it I want
to get some feedback on the interface (or at least, not get negative
feedback).

Cheers,
Tim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Mill  
View profile
 More options Apr 23 2007, 11:20 am
From: Eric Mill <kproject...@gmail.com>
Date: Mon, 23 Apr 2007 15:20:09 -0000
Local: Mon, Apr 23 2007 11:20 am
Subject: Re: Sharing a resource's schema through REST

> Okay.  I see that my first patch was committed this morning (thanks
> DHH) so this is the next logical step.  Note that by itself, this
> change enables nothing without a proper schema definition.  Basically
> it would be trading a little bit of aesthetics (e.g., datetime for
> dateTime) for a little bit of purity (a "standard" type list).

Could you elaborate here a bit and list the changes needed to the
current AR::to_xml method to make it W3C compliant?  This sounds like
a good idea, though I'm curious as to how heavy the aesthetics-
standards tradeoff will be.

> On the subject of XML schemas, the biggest missing piece of the puzzle
> is a way to get the appropriate attributes on the root element for
> records.  I am thinking an interface would look something like

> @people.to_xml(
>   :root_attributes => {
>     'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
>     'xsi:noNamespaceSchemaLocation' => formatted_people_url(:xsd)
>   }
> )

Could this be even simpler?  And, assuming the schema is at /people/
new.xml:

  @people.to_xml(:format => :w3c, :schema => new_person_url(:format
=> :xml))

And maybe, to address Michael's concern, we could support both a
simple and a RelaxNG format for model schemas:

  Person.to_xml(:format => :rng) # Relax NG format
  Person.to_xml # Simpler, Railsy format


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Mill  
View profile
 More options Apr 23 2007, 1:03 pm
From: Eric Mill <em...@thoughtbot.com>
Date: Mon, 23 Apr 2007 17:03:27 -0000
Local: Mon, Apr 23 2007 1:03 pm
Subject: Re: Sharing a resource's schema through REST

> I'm not necessarily sold on the idea of generating a particular schema
> language, or even of generating a schema at all,  but I do know that
> inventing our own is something we should be very wary of.

RelaxNG is a schema for *XML*, not for a *model*.  This schema is only
indirectly used to construct XML POSTs/PUTs, its primary use is for
the client to understand the makeup of your data.  In fact, "Elements"
and "text nodes" aren't the correct conceptual way to describe a data
model.

In fact, consider that a RelaxNG schema would be outright inaccurate
unless you label every single element in the schema as optional.  You
can update as few or as many attributes as you like in a PUT, and you
may never be interested in including data for associations in your
requests, yet you probably want to note all attributes and
associations in a schema for the model.  The way you label elements as
optional in RelaxNG seems to be just "zeroOrMore".  This terminology
should be used only to describe the nature of associations between
data, not just to make sure the flexible nature of REST is described
technically accurately.

So, I think RelaxNG is inappropriate for a simple, flexible
description of a data model, and that we should keep the format as
close to the current AR#to_xml format as possible.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.