Skip to content

Commit

Permalink
fix: handle type field called with 'enum' reserved word (#1983)
Browse files Browse the repository at this point in the history
* fix: handle type field called with 'enum' reserved word

* chore: changeset

* chore: use node20 version of actions/upload-artifact
  • Loading branch information
gabriele-ct committed May 22, 2024
1 parent d55b7e3 commit 2f83a52
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/selfish-experts-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-docs/gatsby-theme-api-docs': patch
'@commercetools-api-specs/test': patch
---

Correctly handle type field when the name is reserved word 'enum'
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ jobs:
CYPRESS_CI: 'true'

- name: Upload e2e artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v3-node20
if: ${{ failure() }}
with:
name: self-learning-e2e-failure-snapshots-${{ github.run_attempt }}
Expand Down
12 changes: 11 additions & 1 deletion api-specs/test/types/enums.raml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ types:
- EnumEntryOne
- EnumEntryTwo
- EnumEntryThree
- enum
(annotations.enumDescriptions):
EnumEntryOne: |
This is the first entry in the EnumTest enumeration.
EnumEntryTwo: |
This is the second entry in the EnumTest enumeration.
EnumEntryThree: |
This is the third entry in the EnumTest enumeration.
enum: |
This is an item called with a reserved word.
EnumPropertiesType:
description: Test data for properties with enums that are of differing primitive types
properties:
Expand Down Expand Up @@ -99,4 +109,4 @@ types:
enumValue9: Group 2
enumValue10: Group 2
enumWithoutDescription: Group 2
enumWithMarkdownDescription: Group 1
enumWithMarkdownDescription: Group 1
17 changes: 14 additions & 3 deletions packages/gatsby-theme-api-docs/src/components/type/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Markdown } from '@commercetools-docs/ui-kit';
import SpacingsStack from '@commercetools-uikit/spacings-stack';
import { DescriptionText, DescriptionParagraph } from '../description';

// @see https://github.com/commercetools/commercetools-docs-kit/blob/d55b7e3a263e918599bd2ac4a18830bd2f1c7d9b/packages/gatsby-transformer-raml/src/utils/type/do-recursion.mjs#L15
// `enum` is converted to `enumeration` as it is a js reserved keyword. This must be taken into account when searching for the enum description.
const enumDescriptionFinder = (enumDesc, value) => {
if (value === 'enum') {
return enumDesc.name === 'enumeration';
}
return enumDesc.name === value;
};

const Enum = ({
values,
enumDescriptions,
Expand Down Expand Up @@ -50,8 +59,8 @@ const Enum = ({
groupList[groupName].map((value) => {
const enumDescription =
enumDescriptions &&
enumDescriptions.find(
(enumDesc) => enumDesc.name === value
enumDescriptions.find((enumDesc) =>
enumDescriptionFinder(enumDesc, value)
);
return (
<React.Fragment key={value}>
Expand Down Expand Up @@ -95,7 +104,9 @@ const Enum = ({
values.map((value) => {
const enumDescription =
enumDescriptions &&
enumDescriptions.find((enumDesc) => enumDesc.name === value);
enumDescriptions.find((enumDesc) =>
enumDescriptionFinder(enumDesc, value)
);

return (
<React.Fragment key={value}>
Expand Down

0 comments on commit 2f83a52

Please sign in to comment.