Commit ea77edd

mo <mo.khan@gmail.com>
2019-01-08 03:54:14
render extension schema attributes in json
1 parent d5183d2
lib/scim/kit/v2/templates/resource.json.jbuilder
@@ -7,6 +7,16 @@ json.external_id external_id
 json.meta do
   render meta, json: json
 end
-dynamic_attributes.values.each do |attribute|
-  render attribute, json: json
+schemas.each do |schema|
+  if schema.core?
+    schema.attributes.each do |type|
+      render dynamic_attributes[type.name.underscore], json: json
+    end
+  else
+    json.set! schema.id do
+      schema.attributes.each do |type|
+        render dynamic_attributes[type.name.underscore], json: json
+      end
+    end
+  end
 end
lib/scim/kit/v2/attribute_type.rb
@@ -35,7 +35,7 @@ module Scim
         attr_reader :uniqueness
 
         def initialize(name:, type: :string)
-          @name = name
+          @name = name.to_s
           @type = type.to_sym
           @description = ''
           @multi_valued = false
lib/scim/kit/v2/schema.rb
@@ -23,6 +23,10 @@ module Scim
           attributes << attribute
         end
 
+        def core?
+          id.include?(Schemas::CORE)
+        end
+
         def self.build(*args)
           item = new(*args)
           yield item
lib/scim/kit/templatable.rb
@@ -13,7 +13,7 @@ module Scim
       end
 
       def to_h
-        JSON.parse(to_json, symbolize_names: true)
+        JSON.parse(to_json, symbolize_names: true).with_indifferent_access
       end
 
       def render(model, options)
spec/scim/kit/v2/resource_spec.rb
@@ -1,8 +1,9 @@
 # frozen_string_literal: true
 
 RSpec.describe Scim::Kit::V2::Resource do
-  subject { described_class.new(schemas: [schema], location: resource_location) }
+  subject { described_class.new(schemas: schemas, location: resource_location) }
 
+  let(:schemas) { [schema] }
   let(:schema) { Scim::Kit::V2::Schema.new(id: Scim::Kit::V2::Schemas::USER, name: 'User', location: FFaker::Internet.uri('https')) }
   let(:resource_location) { FFaker::Internet.uri('https') }
 
@@ -90,4 +91,18 @@ RSpec.describe Scim::Kit::V2::Resource do
     specify { expect(subject.emails).to match_array([{ value: email, primary: true }, { value: other_email, primary: false }]) }
     specify { expect(subject.as_json[:emails]).to match_array([{ value: email, primary: true }, { value: other_email, primary: false }]) }
   end
+
+  context 'with multiple schemas' do
+    let(:schemas) { [schema, extension] }
+    let(:extension) { Scim::Kit::V2::Schema.new(id: extension_id, name: 'Extension', location: FFaker::Internet.uri('https')) }
+    let(:extension_id) { 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User' }
+
+    before do
+      extension.add_attribute(name: :department)
+      subject.department = 'voltron'
+    end
+
+    specify { expect(subject.department).to eql('voltron') }
+    specify { expect(subject.as_json[extension_id][:department]).to eql('voltron') }
+  end
 end