Skip to content

Commit

Permalink
Execute JSON.parse() twice to parse escaped JSON string in `Kubernete…
Browse files Browse the repository at this point in the history
…sService#getContentScope` (v5) (#2680)

Co-authored-by: Thomas Dax <[email protected]>
  • Loading branch information
johnnyomair and thomasdax98 authored Oct 29, 2024
1 parent ae6e9b9 commit 070ec85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/kind-sloths-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-api": patch
---

Fix parsing of `contentScopeAnnotation` in `KubernetesService#getContentScope`
15 changes: 14 additions & 1 deletion packages/api/cms-api/src/kubernetes/kubernetes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ export class KubernetesService {

getContentScope(resource: V1Job | V1CronJob): ContentScope {
const contentScopeAnnotation = resource.metadata?.annotations?.[CONTENT_SCOPE_ANNOTATION];
return contentScopeAnnotation ? JSON.parse(contentScopeAnnotation) : {};

if (contentScopeAnnotation) {
let json = JSON.parse(contentScopeAnnotation);

// the contentScopeAnnotation is an escaped json string (e.g. "{ \"domain\": \"main\", \"language\": \"en\" }")
// therefore JSON.parse() must be executed twice (https://stackoverflow.com/a/25721227)
if (typeof json !== "object") {
json = JSON.parse(json);
}

return json;
}

return {};
}
}

0 comments on commit 070ec85

Please sign in to comment.