Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for query by id when type is Edm.Int64 #40

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ruby_odata/class_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def add_methods(klass)
klass.send :define_method, :id do
metadata = self.__metadata
id = nil
if metadata && metadata[:uri] =~ /\((\d+)\)$/
if metadata && metadata[:uri] =~ /\((\d+)L?\)$/
id = $~[1]
end
return (true if Integer(id) rescue false) ? id.to_i : id
Expand Down
54 changes: 49 additions & 5 deletions lib/ruby_odata/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def initialize(service_uri, options = {})
def method_missing(name, *args)
# Queries
if @collections.include?(name.to_s)
root = "/#{name.to_s}"
root << "(#{args.join(',')})" unless args.empty?
@query = QueryBuilder.new(root, @additional_params)
@query = build_collection_query_object(name,@additional_params, *args)
return @query
# Adds
elsif name.to_s =~ /^AddTo(.*)/
Expand All @@ -42,7 +40,7 @@ def method_missing(name, *args)
super
end
end

# Queues an object for deletion. To actually remove it from the server, you must call save_changes as well.
#
# @param [Object] obj the object to mark for deletion
Expand Down Expand Up @@ -166,7 +164,53 @@ def add_link(parent, nav_prop, child)
end

private


# Constructs a QueryBuilder instance for a collection using the arguments provided.
#
# @param [String] name the name of the collection
# @param [Hash] additional_parameters the additional parameters
# @param [Array] args the arguments to use for query
def build_collection_query_object(name, additional_parameters, *args)
root = "/#{name.to_s}"
if args.empty?
#nothing to add
elsif args.size == 1
if args.first.to_s =~ /\d+/
id_metadata = find_id_metadata(name.to_s)
root << build_id_path(args.first, id_metadata)
else
root << "(#{single_arg})"
end
else
root << "(#{args.join(',')})"
end
QueryBuilder.new(root, additional_parameters)
end

# Finds the metadata associated with the given collection's id property
#
# @param [String] collection_name the name of the collection
def find_id_metadata(collection_name)
collection_data = @collections.fetch(collection_name)
class_metadata = @class_metadata.fetch(collection_data[:type].to_s)
[ 'id', 'Id', 'ID', 'iD' ].each do |k|
return class_metadata[k] if class_metadata[k]
end
return nil
end

# Builds the ID expression of a given id for query
#
# @param [Object] id_value the actual value to be used
# @param [PropertyMetadata] id_metadata the property metadata object for the id
def build_id_path(id_value, id_metadata)
if id_metadata.type == "Edm.Int64"
"(#{id_value}L)"
else
"(#{id_value})"
end
end

def set_options!(options)
@options = options
if @options[:eager_partial].nil?
Expand Down
21 changes: 21 additions & 0 deletions spec/fixtures/edmx_service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="1.0" m:MaxDataServiceVersion="2.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="Model" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="Car">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int64" Nullable="false" p6:StoreGeneratedPattern="Identity" xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation" />
<Property Name="color" Type="Edm.String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="false" />
<Property Name="num_spots" Type="Edm.Int32" />
<Property Name="striped" Type="Edm.Byte" />
</EntityType>
</Schema>
<Schema Namespace="WebApp" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="adoEntities" m:IsDefaultEntityContainer="true" p6:LazyLoadingEnabled="true" xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntitySet Name="Cars" EntityType="Model.Car" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
28 changes: 28 additions & 0 deletions spec/fixtures/edmx_service_cars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://test.com/test.svc/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>http://test.com/test.svc/Cars</id>
<title type="text">Cars</title>
<updated>2013-10-28T19:59:15Z</updated>
<link rel="self" title="Cars" href="Cars" />
<entry>
<id>http://test.com/test.svc/Cars(213L)</id>
<category term="Model.Car" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" title="Car" href="Cars(213L)" />
<title />
<updated>2013-10-28T19:59:15Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:id m:type="Edm.Int64">213</d:id>
<d:color>peach</d:color>
<d:num_spots m:type="Edm.Int32" m:null="true" />
<d:striped m:type="Edm.Byte" m:null="true" />
</m:properties>
</content>
</entry>
</feed>
43 changes: 43 additions & 0 deletions spec/revised_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,47 @@ module OData
end
end
end
describe Service do
it "should handle long keys properly" do
stub_request(:get, "http://test.com/test.svc/$metadata").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
to_return(:status => 200, :body => File.new(File.expand_path("../fixtures/edmx_service.xml", __FILE__)), :headers => {})

svc = OData::Service.new "http://test.com/test.svc/"
meta = svc.class_metadata['Car']['id']
meta.name.should eq 'id'
meta.type.should eq 'Edm.Int64'
meta.nullable.should eq false
meta.fc_target_path.should be_nil
meta.fc_keep_in_content.should be_nil
end

it "should find record with proper id conversion and update it" do
stub_request(:get, "http://test.com/test.svc/$metadata").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
to_return(:status => 200, :body => File.new(File.expand_path("../fixtures/edmx_service.xml", __FILE__)), :headers => {})

stub_request(:get, "http://test.com/test.svc/Cars(213L)").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => File.new(File.expand_path("../fixtures/edmx_service_cars.xml", __FILE__)), :headers => {})
svc = OData::Service.new "http://test.com/test.svc/"

svc.Cars(213)
results = svc.execute
results.size.should == 1
car = results.first
car.id.should eq 213
car.color.should eq "peach"

stub_request(:put, "http://test.com/test.svc/Cars(213L)").
with(:body => "{\"__metadata\":{\"uri\":\"http://test.com/test.svc/Cars(213L)\"},\"id\":\"213\",\"color\":\"red\",\"num_spots\":null,\"striped\":null}",
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'117', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_return(:status => 204, :body => "", :headers => {})

car.color = "red"
svc.update_object(car)
result = svc.save_changes
result.should be_true
end
end
end