OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix status parsing when nil
Oliver Günther42d34a56c3a3
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rbindex f462098a567..8d64bda46dc 100644--- a/app/services/projects/set_attributes_service.rb+++ b/app/services/projects/set_attributes_service.rb@@ -33,7 +33,10 @@ module Projectsprivatedef set_attributes(attributes)- status_attributes = attributes.delete(:status) || {}+ # Delete the status attribute which gets set by representable+ # IFF the status_code is given as null+ attributes.delete(:status)+ status_attributes = attributes.delete(:status_attributes) || {}ret = super(attributes)diff --git a/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.html b/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.htmlindex 5ff9f534078..ba20b5b76fe 100644--- a/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.html+++ b/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.html@@ -1,20 +1,22 @@-<ng-select [items]="availableStatuses"- [(ngModel)]="resource['status']"- bindLabel="name"- bindValue="value"- class="project-status"- (open)="onOpen()"- (close)="onClose()"- (change)="handler.handleUserSubmit()"- [appendTo]="hiddenOverflowContainer"- [hideSelected]="true"- [disabled]="inFlight">- <ng-template ng-label-tmp let-item="item">- <span class="project-status--bulb" [ngClass]="item.colorClass"></span>- <span class="project-status--name" [ngClass]="item.colorClass">{{item.name}}</span>- </ng-template>- <ng-template ng-option-tmp let-item="item" let-index="index" let-search="searchTerm">- <span class="project-status--bulb" [ngClass]="item.colorClass"></span>- <span class="project-status--name" [ngClass]="item.colorClass">{{item.name}}</span>- </ng-template>-</ng-select>\ No newline at end of file+<div (keydown.escape)="handler.handleUserCancel()">+ <ng-select [items]="availableStatuses"+ [(ngModel)]="resource.status"+ bindLabel="name"+ bindValue="value"+ class="project-status"+ (open)="onOpen()"+ (close)="onClose()"+ (change)="handler.handleUserSubmit()"+ [appendTo]="hiddenOverflowContainer"+ [hideSelected]="true"+ [disabled]="inFlight">+ <ng-template ng-label-tmp let-item="item">+ <span class="project-status--bulb" [ngClass]="item.colorClass"></span>+ <span class="project-status--name" [ngClass]="item.colorClass">{{item.name}}</span>+ </ng-template>+ <ng-template ng-option-tmp let-item="item" let-index="index" let-search="searchTerm">+ <span class="project-status--bulb" [ngClass]="item.colorClass"></span>+ <span class="project-status--name" [ngClass]="item.colorClass">{{item.name}}</span>+ </ng-template>+ </ng-select>+</div>\ No newline at end of filediff --git a/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.ts b/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.tsindex 33bd2ee05e8..2b0ff950255 100644--- a/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.ts+++ b/frontend/src/app/modules/fields/edit/field-types/project-status-edit-field.component.ts@@ -72,4 +72,8 @@ export class ProjectStatusEditFieldComponent extends EditFieldComponent implemenpublic onClose() {// Nothing to do}++ public handleKeyDown($event:KeyboardEvent) {+ console.debug($event);+ }}diff --git a/frontend/src/app/modules/grids/grid/grid.component.ts b/frontend/src/app/modules/grids/grid/grid.component.tsindex 2b850b1152a..7b005a702d6 100644--- a/frontend/src/app/modules/grids/grid/grid.component.ts+++ b/frontend/src/app/modules/grids/grid/grid.component.ts@@ -73,7 +73,7 @@ export class GridComponent implements OnDestroy, OnInit {let registration = this.widgetsService.registered.find((reg) => reg.identifier === widget.identifier);if (!registration) {- debugLog(`No widget registered with identifier ${widget.identifier}`);+ // debugLog(`No widget registered with identifier ${widget.identifier}`);return null;} else {diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rbindex 8a046f01228..06a5396d54b 100644--- a/lib/api/v3/projects/project_representer.rb+++ b/lib/api/v3/projects/project_representer.rb@@ -46,6 +46,13 @@ module APIself_link+ def to_h+ # Representable is broken when passing nil as parameters+ # it will set the property :status and :statusExplanation+ # regardless of what the setter actually does+ super.except(:status, :statusExplanation)+ end+link :createWorkPackage,cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do{@@ -163,19 +170,22 @@ module APIdate_time_property :updated_atproperty :status,+ name_source: ->(*) { I18n.t('activerecord.attributes.project/status.code') },render_nil: true,getter: ->(*) {next unless status&.codestatus.code.to_s.tr('_', ' ')},- setter: ->(fragment:, **) {- self.status ||= {}- status[:code] =- if fragment.nil?+ reader: ->(doc:, represented:, **) {+ next unless doc.key?('status')++ represented.status_attributes ||= {}+ represented.status_attributes[:code] =+ if doc['status'].nil?nilelse- fragment.strip.tr(' ', '_').underscore.to_sym+ doc['status'].strip.tr(' ', '_').underscore.to_symend}@@ -186,9 +196,9 @@ module APIobject: self,plain: false)},- setter: ->(fragment:, **) {- self.status ||= {}- status[:explanation] = fragment["raw"]+ setter: ->(fragment:, represented:, **) {+ represented.status_attributes ||= {}+ represented.status_attributes[:explanation] = fragment["raw"]}def _typediff --git a/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rbindex 7c9951a7b31..eedd04ea27f 100644--- a/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb+++ b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb@@ -49,11 +49,11 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' doendit 'updates code' do- project = representer.from_hash(hash)- expect(project.status[:code])+ project_obj = representer.from_hash(hash)+ expect(project_obj.status_attributes[:code]).to eql(:on_track)- expect(project.status[:explanation])+ expect(project_obj.status_attributes[:explanation]).to eql('status code explanation')end@@ -65,14 +65,14 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' doendit 'does not set code' do- project = representer.from_hash(hash)- expect(project.status[:code])+ project_obj = representer.from_hash(hash)+ expect(project_obj.status_attributes[:code]).to be_nilendit 'updates explanation' do- project = representer.from_hash(hash)- expect(project.status[:explanation])+ project_obj = representer.from_hash(hash)+ expect(project_obj.status_attributes[:explanation]).to eql('status code explanation')endend@@ -85,14 +85,14 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' doendit 'does set code' do- project = representer.from_hash(hash)- expect(project.status[:code])+ project_obj = representer.from_hash(hash)+ expect(project_obj.status_attributes[:code]).to eql :off_trackendit 'does not set explanation' do- project = representer.from_hash(hash)- expect(project.status[:explanation])+ project_obj = representer.from_hash(hash)+ expect(project_obj.status_attributes[:explanation]).to be_nilendend@@ -105,13 +105,20 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' doendit 'does set status to nil' do- project = representer.from_hash(hash).to_h+ project_obj = representer.from_hash(hash).to_h- expect(project)- .to be_key(:status)+ expect(project_obj)+ .to have_key(:status_attributes)- expect(project[:status])- .to be_nil+ status_attributes = project_obj[:status_attributes]+ expect(status_attributes)+ .to have_key(:code)++ expect(status_attributes)+ .not_to have_key(:explanation)++ expect(status_attributes[:code])+ .to eq nilendendenddiff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rbindex 2ed09096a2f..a798b585f27 100644--- a/spec/requests/api/v3/project_resource_spec.rb+++ b/spec/requests/api/v3/project_resource_spec.rb@@ -496,6 +496,37 @@ describe 'API v3 Project resource', type: :request, content_type: :json doendend+ context 'with a nil status' do+ let(:body) do+ {+ status: nil,+ statusExplanation: {+ raw: "Some explanation."+ }+ }+ end++ it 'alters the status' do+ expect(last_response.body)+ .to be_json_eql(nil.to_json)+ .at_path('status')++ status = project.status.reload+ expect(status.code).to be_nil+ expect(status.explanation).to eq 'Some explanation.'++ expect(last_response.body)+ .to be_json_eql(+ {+ "format": "markdown",+ "html": "<p>Some explanation.</p>",+ "raw": "Some explanation."+ }.to_json+ )+ .at_path("statusExplanation")+ end+ end+context 'with a status' dolet(:body) do{diff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex d25b9f97030..64081f44b72 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -37,8 +37,8 @@ describe Projects::SetAttributesService, type: :model doallow(contract).to receive(:new)- .with(project, user)- .and_return(contract_instance)+ .with(project, user)+ .and_return(contract_instance)contractend@@ -69,11 +69,11 @@ describe Projects::SetAttributesService, type: :model dobefore doallow(project).to receive(:valid?)- .and_return(project_valid)+ .and_return(project_valid)expect(contract_instance).to receive(:validate)- .and_return(contract_valid)+ .and_return(contract_valid)endsubject { instance.call(call_attributes) }@@ -102,7 +102,7 @@ describe Projects::SetAttributesService, type: :model doendcontext 'identifier default value' do- context 'with a default identifier configured', with_settings: { sequential_project_identifiers: true } do+ context 'with a default identifier configured', with_settings: {sequential_project_identifiers: true} docontext 'with an identifier provided' dolet(:call_attributes) do{@@ -120,7 +120,7 @@ describe Projects::SetAttributesService, type: :model doit 'sets a default identifier' doallow(Project).to receive(:next_identifier)- .and_return('ipsum')+ .and_return('ipsum')expect(subject.result.identifier).to eql 'ipsum'@@ -128,7 +128,7 @@ describe Projects::SetAttributesService, type: :model doendend- context 'without a default identifier configured', with_settings: { sequential_project_identifiers: false } do+ context 'without a default identifier configured', with_settings: {sequential_project_identifiers: false} docontext 'with an identifier provided' dolet(:call_attributes) do{@@ -146,7 +146,7 @@ describe Projects::SetAttributesService, type: :model doit 'stays nil' doallow(Project).to receive(:next_identifier)- .and_return('ipsum')+ .and_return('ipsum')expect(subject.result.identifier).to be_nil@@ -155,7 +155,7 @@ describe Projects::SetAttributesService, type: :model doendend- context 'public default value', with_settings: { default_projects_public: true } do+ context 'public default value', with_settings: {default_projects_public: true} docontext 'with a value for is_public provided' dolet(:call_attributes) do{@@ -177,7 +177,7 @@ describe Projects::SetAttributesService, type: :model doendend- context 'enabled_module_names default value', with_settings: { default_projects_modules: ['lorem', 'ipsum'] } do+ context 'enabled_module_names default value', with_settings: {default_projects_modules: ['lorem', 'ipsum']} docontext 'with a value for enabled_module_names provided' dolet(:call_attributes) do{@@ -220,7 +220,7 @@ describe Projects::SetAttributesService, type: :model dobefore doallow(Type).to receive(:default)- .and_return default_types+ .and_return default_typesendcontext 'with a value for types provided' do@@ -259,8 +259,10 @@ describe Projects::SetAttributesService, type: :model docontext 'with a value provided' dolet(:call_attributes) do{- status: 'on track',- statusExplanation: 'A magic dwells in each beginning.'+ status_attributes: {+ code: 'on_track',+ explanation: 'A magic dwells in each beginning.'+ }}end@@ -288,8 +290,10 @@ describe Projects::SetAttributesService, type: :model docontext 'with a value provided' dolet(:call_attributes) do{- status: 'on_track',- statusExplanation: 'A magic dwells in each beginning.'+ status_attributes: {+ code: 'on_track',+ explanation: 'A magic dwells in each beginning.'+ }}end@@ -312,8 +316,10 @@ describe Projects::SetAttributesService, type: :model docontext 'with an invalid code' dolet(:call_attributes) do{- status: 'bogus',- statusExplanation: 'A magic dwells in each beginning.'+ status_attributes: {+ code: 'bogus',+ explanation: 'A magic dwells in each beginning.'+ }}end@@ -345,8 +351,10 @@ describe Projects::SetAttributesService, type: :model docontext 'with a value provided' dolet(:call_attributes) do{- status: 'at_risk',- statusExplanation: 'Still some magic there.'+ status_attributes: {+ code: 'at_risk',+ explanation: 'Still some magic there.'+ }}end