diff --git a/frontend/__tests__/components/operator-lifecycle-manager/create-crd-yaml.spec.tsx b/frontend/__tests__/components/operator-lifecycle-manager/create-crd-yaml.spec.tsx index 55d60f38aab8..e7ca08dc8693 100644 --- a/frontend/__tests__/components/operator-lifecycle-manager/create-crd-yaml.spec.tsx +++ b/frontend/__tests__/components/operator-lifecycle-manager/create-crd-yaml.spec.tsx @@ -41,6 +41,16 @@ describe(CreateCRDYAML.displayName, () => { expect(createYAML.props().template).toEqual(safeDump(testResourceInstance)); }); + it('handles invalid JSON example object on annotation', () => { + const data = _.cloneDeep(testClusterServiceVersion); + data.metadata.annotations = {'alm-examples': 'invalid === true'}; + wrapper = wrapper.setProps({ClusterServiceVersion: {loaded: true, data}} as any); + + const createYAML = wrapper.find(Firehose).childAt(0).dive(); + + expect(createYAML.props().template).toEqual(null); + }); + it('does not render YAML editor component if ClusterServiceVersion has not loaded yet', () => { wrapper = wrapper.setProps({ClusterServiceVersion: {loaded: false}} as any); diff --git a/frontend/public/components/operator-lifecycle-manager/create-crd-yaml.tsx b/frontend/public/components/operator-lifecycle-manager/create-crd-yaml.tsx index 1005d11ed097..3ebc0ac77748 100644 --- a/frontend/public/components/operator-lifecycle-manager/create-crd-yaml.tsx +++ b/frontend/public/components/operator-lifecycle-manager/create-crd-yaml.tsx @@ -19,10 +19,12 @@ export const CreateCRDYAML: React.SFC = (props) => { const Create = (createProps: {ClusterServiceVersion: {loaded: boolean, data: ClusterServiceVersionKind}}) => { if (createProps.ClusterServiceVersion.loaded && createProps.ClusterServiceVersion.data) { - const templates = _.get(createProps.ClusterServiceVersion.data.metadata.annotations, annotationKey, '[]'); - const templateObj = (JSON.parse(templates) as K8sResourceKind[]) - .find(obj => referenceFor(obj) === props.match.params.plural); - const template = templateObj ? _.attempt(() => safeDump(templateObj)) : null; + const templatesJSON = _.get(createProps.ClusterServiceVersion.data.metadata.annotations, annotationKey, '[]'); + const template = _.attempt(() => safeDump((JSON.parse(templatesJSON) as K8sResourceKind[]).find(obj => referenceFor(obj) === props.match.params.plural))); + if (_.isError(template)) { + // eslint-disable-next-line no-console + console.error('Error parsing example JSON from annotation. Falling back to default.'); + } return ; }