diff --git a/lib/api/v3/work_packages/form/form_representer.rb b/lib/api/v3/work_packages/form/form_representer.rb index e495f670a1..14950fadfc 100644 --- a/lib/api/v3/work_packages/form/form_representer.rb +++ b/lib/api/v3/work_packages/form/form_representer.rb @@ -89,7 +89,8 @@ module API getter: -> (*) { schema = Schema::WorkPackageSchema.new(work_package: represented) Schema::WorkPackageSchemaRepresenter.new(schema, - current_user: @current_user) + form_embedded: true, + current_user: @current_user) } property :validation_errors, embedded: true, exec_context: :decorator diff --git a/lib/api/v3/work_packages/schema/work_package_schema.rb b/lib/api/v3/work_packages/schema/work_package_schema.rb index 5e9f72852b..dac4eea78e 100644 --- a/lib/api/v3/work_packages/schema/work_package_schema.rb +++ b/lib/api/v3/work_packages/schema/work_package_schema.rb @@ -35,7 +35,7 @@ module API module WorkPackages module Schema class WorkPackageSchema - attr_reader :project + attr_reader :project, :type def initialize(work_package: nil, project: nil, type: nil) raise ArgumentError unless work_package || (project && type) diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb index 10703d5d7c..23f81dbc3b 100644 --- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb +++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb @@ -73,7 +73,7 @@ module API representer.required = required representer.writable = writable - if represented.defines_assignable_values? + if @form_embedded && represented.defines_assignable_values? representer.allowed_values_href = instance_eval(&href_callback) end @@ -81,6 +81,20 @@ module API } end + def initialize(represented, context = {}) + @form_embedded = context[:form_embedded] + + super + end + + link :self do + path = api_v3_paths.work_package_schema(represented.project.id, represented.type.id) + + unless @form_embedded + { href: path } + end + end + schema :_type, type: 'MetaType', title: I18n.t('api_v3.attributes._type'), @@ -175,7 +189,7 @@ module API } }) - if represented.defines_assignable_values? + if @form_embedded && represented.defines_assignable_values? representer.allowed_values = assignable_statuses end @@ -198,7 +212,7 @@ module API representer.required = false - if represented.defines_assignable_values? + if @form_embedded && represented.defines_assignable_values? representer.allowed_values = represented.assignable_categories end @@ -222,7 +236,7 @@ module API representer.required = false - if represented.defines_assignable_values? + if @form_embedded && represented.defines_assignable_values? representer.allowed_values = represented.assignable_versions end @@ -244,7 +258,7 @@ module API } }) - if represented.defines_assignable_values? + if @form_embedded && represented.defines_assignable_values? representer.allowed_values = represented.assignable_priorities end diff --git a/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb index 28977b44b7..c2f74b9f28 100644 --- a/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb @@ -29,6 +29,8 @@ require 'spec_helper' describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do + include API::V3::Utilities::PathHelper + let(:work_package) { FactoryGirl.build(:work_package) } let(:current_user) { FactoryGirl.build(:user, member_in_project: work_package.project) @@ -41,6 +43,10 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do context 'generation' do subject(:generated) { representer.to_json } + shared_context 'embedded in form' do + let(:representer) { described_class.new(schema, form_embedded: true, current_user: current_user) } + end + shared_context 'no allowed values' do before do allow(schema).to receive(:defines_assignable_values?).and_return(false) @@ -113,6 +119,23 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end end + describe '_links' do + it 'should link to self' do + path = api_v3_paths.work_package_schema(work_package.project.id, work_package.type.id) + + is_expected.to be_json_eql(path.to_json).at_path('_links/self/href') + end + + context 'when the schema is embedded in a form' do + include_context 'embedded in form' + + it 'should not link to self' do + + is_expected.to_not have_json_path('_links/self') + end + end + end + describe '_type' do it_behaves_like 'has basic schema properties' do let(:path) { '_type' } @@ -272,6 +295,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end describe 'status' do + include_context 'embedded in form' + it_behaves_like 'has basic schema properties' do let(:path) { 'status' } let(:type) { 'Status' } @@ -310,6 +335,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end describe 'categories' do + include_context 'embedded in form' + it_behaves_like 'has basic schema properties' do let(:path) { 'category' } let(:type) { 'Category' } @@ -348,6 +375,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end describe 'versions' do + include_context 'embedded in form' + it_behaves_like 'has basic schema properties' do let(:path) { 'version' } let(:type) { 'Version' } @@ -386,6 +415,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end describe 'priorities' do + include_context 'embedded in form' + it_behaves_like 'has basic schema properties' do let(:path) { 'priority' } let(:type) { 'Priority' } @@ -424,6 +455,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do end describe 'responsible and assignee' do + include_context 'embedded in form' + let(:base_href) { "/api/v3/projects/#{work_package.project.id}" } describe 'assignee' do