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
[#40942] documented API behaviour for relation creation
Eric Schubert6ddbead33043
diff --git a/docs/api/apiv3/components/examples/relation_collection_response.yml b/docs/api/apiv3/components/examples/relation_collection_response.ymlindex 389b0947f67..2b780773791 100644--- a/docs/api/apiv3/components/examples/relation_collection_response.yml+++ b/docs/api/apiv3/components/examples/relation_collection_response.yml@@ -1,5 +1,5 @@-# Example: Relation Collection----+description: |-+ Simple response of a relation collection.value:_type: Collectiontotal: 40diff --git a/docs/api/apiv3/components/examples/relation_create_request.yml b/docs/api/apiv3/components/examples/relation_create_request.ymlnew file mode 100644index 00000000000..0e56c6da1e7--- /dev/null+++ b/docs/api/apiv3/components/examples/relation_create_request.yml@@ -0,0 +1,9 @@+description: |-+ Example request to create a relation between two work packages.+value:+ type: follows+ lag: 2+ description: Build hangar AFTER improving external skeleton of Death Star.+ _links:+ to:+ href: /api/v3/work_packages/1337diff --git a/docs/api/apiv3/components/examples/relation_response.yml b/docs/api/apiv3/components/examples/relation_response.ymlindex 80b5035789a..702a2b3bc5b 100644--- a/docs/api/apiv3/components/examples/relation_response.yml+++ b/docs/api/apiv3/components/examples/relation_response.yml@@ -1,5 +1,5 @@-# Example: Relation----+description: |-+ Simple response of a 'duplicated by' relation between two work packages.value:_type: Relationid: 650diff --git a/docs/api/apiv3/components/examples/relation_update_request.yml b/docs/api/apiv3/components/examples/relation_update_request.ymlnew file mode 100644index 00000000000..7c9ee2cd164--- /dev/null+++ b/docs/api/apiv3/components/examples/relation_update_request.yml@@ -0,0 +1,4 @@+description: |-+ Example request to update the relation's description.+value:+ description: This is the new description.diff --git a/docs/api/apiv3/components/schemas/relation_collection_model.yml b/docs/api/apiv3/components/schemas/relation_collection_model.ymlindex 9207cce484d..2baa2587cd0 100644--- a/docs/api/apiv3/components/schemas/relation_collection_model.yml+++ b/docs/api/apiv3/components/schemas/relation_collection_model.yml@@ -27,4 +27,4 @@ allOf:elements:type: arrayitems:- $ref: './relation_model.yml'+ $ref: './relation_read_model.yml'diff --git a/docs/api/apiv3/components/schemas/relation_model.yml b/docs/api/apiv3/components/schemas/relation_read_model.ymlsimilarity index 69%rename from docs/api/apiv3/components/schemas/relation_model.ymlrename to docs/api/apiv3/components/schemas/relation_read_model.ymlindex 2913a5aef6d..a31fd117f2b 100644--- a/docs/api/apiv3/components/schemas/relation_model.yml+++ b/docs/api/apiv3/components/schemas/relation_read_model.yml@@ -1,9 +1,6 @@-# Schema: RelationModel+# Schema: RelationReadModel---type: object-required:- - _type- - idproperties:_type:type: string@@ -14,7 +11,7 @@ properties:description: Relation IDname:type: string- description: The internationalized name of this type of relation+ description: The internationalised name of this type of relationtype:type: stringdescription: The relation type.@@ -47,13 +44,13 @@ properties:- requireddescription:type:- - string- - null+ - 'string'+ - 'null'description: A descriptive text for the relation.lag:type:- - integer- - null+ - 'integer'+ - 'null'description: The lag in days between closing of `from` and start of `to`minimum: 0_embedded:@@ -65,87 +62,55 @@ properties:$ref: './work_package_model.yml'_links:type: object- required:- - self- - schema- - from- - toproperties:self:allOf:- $ref: './link.yml'- description: |-This relation-+**Resource**: Relation-+# Conditions-+**Permission**: view work packagesupdateImmediately:allOf:- $ref: './link.yml'- description: |-Updates the relation between two work packages-+# Conditions-+**Permission**: manage work package relationsdelete:allOf:- $ref: './link.yml'- description: |-Destroys the relation between the two work packages-+# Conditions-+**Permission**: manage work package relationsfrom:allOf:- $ref: './link.yml'- description: |-The emanating work package-+**Resource**: WorkPackage-+# Conditions-+**Permission**: view work packagesto:allOf:- $ref: './link.yml'- description: |-The work package the relation ends in-+**Resource**: WorkPackage-+# Conditions-- **Permission**: view work packages-example:- _links:- self:- href: "/api/v3/relations/1"- update:- href: "/api/v3/relations/1/form"- method: POST- updateImmediately:- href: "/api/v3/relations/1"- method: PATCH- delete:- href: "/api/v3/relations/1"- method: DELETE- from:- href: "/api/v3/work_packages/42"- title: Steel Delivery- to:- href: "/api/v3/work_packages/84"- title: Bending the steel- _type: Relation- id: 1- name: precedes- type: precedes- reverseType: follows- description: We can't bend the steel before it's been delivered!- lag: 0+ **Permission**: view work packagesdiff --git a/docs/api/apiv3/components/schemas/relation_write_model.yml b/docs/api/apiv3/components/schemas/relation_write_model.ymlnew file mode 100644index 00000000000..09a6926c255--- /dev/null+++ b/docs/api/apiv3/components/schemas/relation_write_model.yml@@ -0,0 +1,47 @@+# Schema: RelationWriteModel+---+type: object+required:+ - _links+ - type+properties:+ type:+ type: string+ description: The relation type.+ enum:+ - relates+ - duplicates+ - duplicated+ - blocks+ - blocked+ - precedes+ - follows+ - includes+ - partof+ - requires+ - required+ description:+ type:+ - 'string'+ - 'null'+ description: A descriptive text for the relation.+ lag:+ type:+ - 'integer'+ - 'null'+ description: The lag in days between closing of `from` and start of `to`+ minimum: 0+ _links:+ type: object+ properties:+ to:+ allOf:+ - $ref: './link.yml'+ - description: |-+ The work package the relation ends in. Only available on relation creation, not on update.++ **Resource**: WorkPackage++ # Conditions++ **Permission**: view work packagesdiff --git a/docs/api/apiv3/openapi-spec.yml b/docs/api/apiv3/openapi-spec.ymlindex e097cab19c2..1bcbc69579d 100644--- a/docs/api/apiv3/openapi-spec.yml+++ b/docs/api/apiv3/openapi-spec.yml@@ -521,8 +521,12 @@ components:$ref: "./components/examples/project_collection.yml"RelationCollectionResponse:$ref: "./components/examples/relation_collection_response.yml"+ RelationCreateRequest:+ $ref: "./components/examples/relation_create_request.yml"RelationResponse:$ref: "./components/examples/relation_response.yml"+ RelationUpdateRequest:+ $ref: "./components/examples/relation_update_request.yml"StorageNextcloudResponse:$ref: "./components/examples/storage-nextcloud-response.yml"StorageNextcloudResponseForCreation:@@ -757,8 +761,10 @@ components:"$ref": "./components/schemas/query_update_form.yml"RelationCollectionModel:"$ref": "./components/schemas/relation_collection_model.yml"- RelationModel:- "$ref": "./components/schemas/relation_model.yml"+ RelationReadModel:+ "$ref": "./components/schemas/relation_read_model.yml"+ RelationWriteModel:+ "$ref": "./components/schemas/relation_write_model.yml"RevisionModel:"$ref": "./components/schemas/revision_model.yml"RevisionsModel:diff --git a/docs/api/apiv3/paths/relation.yml b/docs/api/apiv3/paths/relation.ymlindex dc4227ccc4d..88666f57e04 100644--- a/docs/api/apiv3/paths/relation.yml+++ b/docs/api/apiv3/paths/relation.yml@@ -23,7 +23,7 @@ get:response:$ref: '../components/examples/relation_response.yml'schema:- $ref: '../components/schemas/relation_model.yml'+ $ref: '../components/schemas/relation_read_model.yml''404':description: |-Returned if the relation does not exist or the client does not have sufficient permissions to see it.@@ -104,6 +104,8 @@ patch:Note that changing the `type` of a relation invariably also changes the respective `reverseType` as well as the"name" of it. The returned Relation object will reflect that change. For instance if you change a Relation's`type` to "follows" then the `reverseType` will be changed to `precedes`.++ It is not allowed to change a relation's involved work packages.parameters:- description: Relation IDexample: '1'@@ -112,13 +114,21 @@ patch:required: trueschema:type: integer+ requestBody:+ content:+ application/json:+ schema:+ $ref: '../components/schemas/relation_write_model.yml'+ examples:+ request:+ $ref: '../components/examples/relation_update_request.yml'responses:'200':description: OKcontent:application/hal+json:schema:- $ref: '../components/schemas/relation_model.yml'+ $ref: '../components/schemas/relation_read_model.yml'examples:response:$ref: '../components/examples/relation_response.yml'diff --git a/docs/api/apiv3/paths/work_package_relations.yml b/docs/api/apiv3/paths/work_package_relations.ymlindex 35900c5d394..982669326bb 100644--- a/docs/api/apiv3/paths/work_package_relations.yml+++ b/docs/api/apiv3/paths/work_package_relations.yml@@ -1,88 +1,85 @@# /api/v3/work_packages/{id}/relations----get:- parameters:- - description: Work package id- example: '1'- in: path- name: id- required: true- schema:- type: integer- responses:- '302':- content:- text/plain:- schema:- type: string- examples:- response:- value: 'You are being redirected to /api/v3/relations?involved={work_package_id}'- description: Found- headers:- Location:- schema:- type: string- tags:- - Work Packages- description: Lists all relations this work package is involved in.- operationId: List_relations- summary: List relationspost:+ summary: Create relation+ operationId: create_relation+ tags:+ - Relations+ description: |-+ Create a work package relation on the given work package. A successful creation will result in a relation between+ two work packages, thus appearing on both involved work package resources.parameters:- - description: Work package id- example: '1'- in: path- name: id- required: true- schema:- type: integer+ - name: id+ description: Work package id+ example: '1'+ in: path+ required: true+ schema:+ type: integer+ requestBody:+ content:+ application/json:+ schema:+ $ref: '../components/schemas/relation_write_model.yml'+ examples:+ request:+ $ref: '../components/examples/relation_create_request.yml'responses:'201':description: Created- headers: {}+ content:+ application/hal+json:+ schema:+ $ref: '../components/schemas/relation_read_model.yml'+ examples:+ 'Simple response':+ $ref: '../components/examples/relation_response.yml''400':- $ref: "../components/responses/invalid_request_body.yml"+ $ref: '../components/responses/invalid_request_body.yml''403':+ description: |-+ Returned if the client does not have sufficient permissions.++ **Required permission:** manage work package relationscontent:application/hal+json:schema:- $ref: "../components/schemas/error_response.yml"+ $ref: '../components/schemas/error_response.yml'examples:response:value:_type: ErrorerrorIdentifier: urn:openproject-org:api:v3:errors:MissingPermissionmessage: You are not allowed to create a relation.- description: |-- Returned if the client does not have sufficient permissions.-- **Required permission:** manage work package relations- headers: {}'409':+ description: |-+ Returned if there already exists a relation between the given work packages of **any** type+ or if the relation is not allowed.content:application/hal+json:schema:- $ref: "../components/schemas/error_response.yml"+ $ref: '../components/schemas/error_response.yml'examples:response:value:_type: ErrorerrorIdentifier: urn:openproject-org:api:v3:errors:UpdateConflictmessage: Couldn't update the resource because of conflicting modifications.- description: |-- Returned if there already exists a relation between the given work packages of **any** type- or if the relation is not allowed.- headers: {}'406':- $ref: "../components/responses/missing_content_type.yml"+ $ref: '../components/responses/missing_content_type.yml''415':- $ref: "../components/responses/unsupported_media_type.yml"+ $ref: '../components/responses/unsupported_media_type.yml''422':+ description: |-+ Returned if:++ - the client tries to write a read-only property (`PropertyIsReadOnly`)+ - a constraint for a property was violated (`PropertyConstraintViolation`)+ - the client provides a link to an invalid resource (`ResourceTypeMismatch`)content:application/hal+json:schema:- $ref: "../components/schemas/error_response.yml"+ $ref: '../components/schemas/error_response.yml'examples:response:value:@@ -92,20 +89,3 @@ post:_type: ErrorerrorIdentifier: urn:openproject-org:api:v3:errors:PropertyConstraintViolationmessage: Lag must be a number greater than or equal to 0- description: |-- Returned if:-- * the client tries to write a read-only property (`PropertyIsReadOnly`)-- * a constraint for a property was violated (`PropertyConstraintViolation`)-- * the client provides a link to an invalid resource (`ResourceTypeMismatch`)- headers: {}- tags:- - Work Packages- description: |-- When calling this endpoint the client provides a single object, containing at least the properties and links that are required, in the body.- The required fields of a Relation can be found in its schema, which is embedded in the respective form.- Note that it is only allowed to provide properties or links supporting the write operation.- operationId: Create_Relation- summary: Create Relationdiff --git a/lib/api/v3/work_packages/work_package_relations_api.rb b/lib/api/v3/work_packages/work_package_relations_api.rbindex 1843f5df9c7..37ce9c3a9c8 100644--- a/lib/api/v3/work_packages/work_package_relations_api.rb+++ b/lib/api/v3/work_packages/work_package_relations_api.rb@@ -33,28 +33,19 @@ module APIhelpers ::API::V3::Relations::RelationsHelperresources :relations do- ##- # @todo Redirect to relations endpoint as soon as `list relations` API endpoint- # including filters is complete.get do- query = ::Queries::Relations::RelationQuery.new(user: current_user)+ filters = [{ involved: { operator: "=", values: [@work_package.id.to_s] } }]+ url = api_v3_paths.path_for(:relations, filters:)- relations = query- .where(:involved, "=", @work_package.id)- .results- .includes(::API::V3::Relations::RelationCollectionRepresenter.to_eager_load)-- ::API::V3::Relations::RelationCollectionRepresenter.new(- relations,- self_link: api_v3_paths.work_package_relations(@work_package.id),- current_user:- )+ redirect url, body: "The requested resource is deprecated and permanently moved to #{url}"+ status 308endpost &::API::V3::Utilities::Endpoints::Create.new(model: Relation,params_modifier: ->(params) do- params.merge(send_notifications: (params[:notify] != "false"))+ params.merge(send_notifications: (params[:notify] != "false"),+ from: @work_package)end).mountend