From 64ee76f78c271112f801416c9c3bddad10910ba6 Mon Sep 17 00:00:00 2001 From: siwei xu Date: Mon, 16 Sep 2024 06:53:29 -0400 Subject: [PATCH] CMR-9848 Return 400 failure status only when all the associations/dissociations fail. --- search-app/docs/api.md | 23 ++++++++++--------- search-app/src/cmr/search/api/association.clj | 8 +++---- .../cmr/search/api/generic_association.clj | 8 +++---- search-app/src/cmr/search/api/tags.clj | 8 +++---- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/search-app/docs/api.md b/search-app/docs/api.md index f87d85dfc7..f31b184630 100644 --- a/search-app/docs/api.md +++ b/search-app/docs/api.md @@ -3771,7 +3771,7 @@ Content-Length: 168 ] ``` -On occasions when tag association cannot be processed at all due to invalid input, tag association request will return failure status code 400 with the appropriate error message. +On occasions when tag association cannot be processed at all due to invalid input, tag association request will return status code 200 with appropriate error messages for each failed association. If all the associations in the request fail, a 400 failure status code will be returned. #### Associating Collections with a Tag by query @@ -3857,7 +3857,7 @@ curl -XDELETE -i -H "Content-Type: application/json" -H "Authorization: Bearer X {"concept_id": "C1200000006-PROV1"}, {"concept_id": "C1200000007-PROV1"}]' -HTTP/1.1 400 Bad Request +HTTP/1.1 200 OK Content-Type: application/json;charset=ISO-8859-1 Content-Length: 168 @@ -3890,7 +3890,7 @@ Content-Length: 168 ] ``` -On occasions when tag dissociation cannot be processed at all due to invalid input, tag dissociation request will return a failure status code with the appropriate error message. +On occasions when tag dissociation cannot be processed at all due to invalid input, tag dissociation request will return status code 200 with appropriate error messages for each failed dissociation. If all the dissociations in the request fail, a 400 failure status code will be returned. #### Dissociating a Tag from Collections by query @@ -4653,7 +4653,7 @@ curl -XPOST -i -H "Content-Type: application/json" -H "Authorization: Bearer XXX '[{"concept_id": "C1200000005-PROV1", "data": {"order_option": "OO1200445588-PROV1"}}, {"concept_id": "C1200000006-PROV1"}]' -HTTP/1.1 400 BAD REQUEST +HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 168 @@ -4678,20 +4678,21 @@ Content-Length: 168 ] ``` -On occasions when service association cannot be processed at all due to invalid input, service association request will return a failure status code with the appropriate error message. +On occasions when service association cannot be processed at all due to invalid input, service association request will return status code 200 with appropriate error messages for each failed association. If all the associations in the request fail, a 400 failure status code will be returned. #### Service Dissociation A service identified by its concept id can be dissociated from collections through a list of collection concept revisions similar to service association requests. Service dissociation requires that user has update permission on INGEST_MANAGEMENT_ACL for either the collection's provider, or the service's provider. +Service dissociation ``` curl -XDELETE -i -H "Content-Type: application/json" -H "Authorization: Bearer XXXXX" %CMR-ENDPOINT%/services/S1200000008-PROV1/associations -d \ '[{"concept_id": "C1200000005-PROV1"}, {"concept_id": "C1200000006-PROV1"}, {"concept_id": "C1200000007-PROV1"}]' -HTTP/1.1 400 BAD REQUEST +HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 168 @@ -4732,7 +4733,7 @@ Content-Length: 168 ] ``` -On occasions when service dissociation cannot be processed at all due to invalid input, service dissociation request will return a failure status code with the appropriate error message. +On occasions when service dissociation cannot be processed at all due to invalid input, service dissociation request will return status code 200 with appropriate error messages for each failed dissociation. If all the dissociations in the request fail, a 400 failure status code will be returned. ### Tool @@ -5030,7 +5031,7 @@ curl -XPOST -i -H "Content-Type: application/json" -H "Authorization: Bearer XXX '[{"concept_id": "C1200000005-PROV1"}, {"concept_id": "C1200000006-PROV1"}]' -HTTP/1.1 400 BAD REQUEST +HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 168 @@ -5064,7 +5065,7 @@ Content-Length: 168 ] ``` -On occasions when tool association cannot be processed at all due to invalid input, tool association request will return a failure status code with the appropriate error message. +On occasions when tool association cannot be processed at all due to invalid input, tool association request will return status code 200 with appropriate error messages for each failed association. If all the associations in the request fail, a 400 failure status code will be returned. #### Tool Dissociation @@ -5077,7 +5078,7 @@ curl -XDELETE -i -H "Content-Type: application/json" -H "Authorization: Bearer X {"concept_id": "C1200000006-PROV1"}, {"concept_id": "C1200000007-PROV1"}]' -HTTP/1.1 400 BAD REQUEST +HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 168 @@ -5118,7 +5119,7 @@ Content-Length: 168 ] ``` -On occasions when tool dissociation cannot be processed at all due to invalid input, tool dissociation request will return a failure status code with the appropriate error message. +On occasions when tool dissociation cannot be processed at all due to invalid input, tool dissociation request will return status code 200 with appropriate error messages for each failed dissociation. If all the associations in the request fail, a 400 failure status code will be returned. ### Subscription diff --git a/search-app/src/cmr/search/api/association.clj b/search-app/src/cmr/search/api/association.clj index 721a730737..ef0d62e92a 100644 --- a/search-app/src/cmr/search/api/association.clj +++ b/search-app/src/cmr/search/api/association.clj @@ -24,10 +24,10 @@ :body (json/generate-string (util/snake-case-data data)) :headers {"Content-Type" mt/json}})) -(defn- results-contain-errors? - "Returns true if the results contain :errors" +(defn- all-results-contain-errors? + "Returns true if the all results contain :errors" [results] - (seq (filter #(some? (:errors %)) results))) + (not (some #(nil? (:errors %)) results))) (defn association-results->status-code "Check for concept-types requiring error status to be returned. This is currently :service and :variable @@ -35,7 +35,7 @@ any are errors are present. Otherwise it will return 200" [concept-type results] (if (some #{concept-type} '(:variable :service :tool)) - (if (results-contain-errors? results) + (if (all-results-contain-errors? results) 400 200) 200)) diff --git a/search-app/src/cmr/search/api/generic_association.clj b/search-app/src/cmr/search/api/generic_association.clj index 0731d3b011..6e40ee5074 100644 --- a/search-app/src/cmr/search/api/generic_association.clj +++ b/search-app/src/cmr/search/api/generic_association.clj @@ -24,15 +24,15 @@ :body (json/generate-string (util/snake-case-data data)) :headers {"Content-Type" mt/json}})) -(defn- results-contain-errors? - "Returns true if the results contain :errors" +(defn- all-results-contain-errors? + "Returns true if every single result contains :errors" [results] - (seq (filter #(some? (:errors %)) results))) + (not (some #(nil? (:errors %)) results))) (defn- results->status-code "Return status code depending on if results contains error." [results] - (if (results-contain-errors? results) + (if (all-results-contain-errors? results) 400 200)) diff --git a/search-app/src/cmr/search/api/tags.clj b/search-app/src/cmr/search/api/tags.clj index 65e187f5f2..e5c77a6c9d 100644 --- a/search-app/src/cmr/search/api/tags.clj +++ b/search-app/src/cmr/search/api/tags.clj @@ -18,15 +18,15 @@ [headers] (mt/extract-header-mime-type #{mt/json} headers "content-type" true)) -(defn- has-error? - "Whether there's an error in [data]." +(defn- all-data-contain-errors? + "Whether all entries in data contain errors." [data] - (some #(contains? % :errors) data)) + (not (some #(nil? (:errors %)) data))) (defn tag-api-response "Creates a successful tag response with the given data response" ([data] - (if (has-error? data) + (if (all-data-contain-errors? data) (tag-api-response 400 data) (tag-api-response 200 data))) ([status-code data]