diff --git a/components/RestRedirect.tsx b/components/RestRedirect.tsx index a7bcc9ae2ced..8068fe147136 100644 --- a/components/RestRedirect.tsx +++ b/components/RestRedirect.tsx @@ -13,14 +13,21 @@ export function RestRedirect() { const router = useRouter() const { currentVersion } = useVersion() const { allVersions } = useMainContext() - const apiVersions = allVersions[currentVersion].apiVersions - const latestApiVersion = allVersions[currentVersion].latestApiVersion + const validApiVersions = allVersions[currentVersion].apiVersions + const isReleaseVersioned = allVersions[currentVersion].apiVersions.length > 0 + const latestValidApiVersion = allVersions[currentVersion].latestApiVersion + const queryApiVersion = router.query.apiVersion useEffect(() => { - if (allVersions[currentVersion].apiVersions.length > 0 && !router.query.apiVersion) { + if ( + isReleaseVersioned && + (!queryApiVersion || !validApiVersions.includes(queryApiVersion as string)) + ) { const versionCookie = Cookies.get(API_VERSION_COOKIE_NAME) const date = - versionCookie && apiVersions.includes(versionCookie) ? versionCookie : latestApiVersion + versionCookie && validApiVersions.includes(versionCookie) + ? versionCookie + : latestValidApiVersion const hash = router.asPath.split('#')[1] const [asPathRoot, asPathQuery = ''] = router.asPath.split('#')[0].split('?') const params = new URLSearchParams(asPathQuery) diff --git a/components/sidebar/ApiVersionPicker.tsx b/components/sidebar/ApiVersionPicker.tsx index d09cc10909a8..289da6779d5b 100644 --- a/components/sidebar/ApiVersionPicker.tsx +++ b/components/sidebar/ApiVersionPicker.tsx @@ -43,10 +43,15 @@ export const ApiVersionPicker = ({ variant, width }: Props) => { const { t } = useTranslation(['products']) const basePath = router.asPath.split('#')[0].split('?')[0] // Get current date from cookie, query path, or lastly set it to latest rest version date - const currentDate = - router.query.apiVersion && typeof router.query.apiVersion === 'string' - ? router.query.apiVersion - : allVersions[currentVersion].latestApiVersion + const isValidApiVersion = + (router.query.apiVersion && + typeof router.query.apiVersion === 'string' && + allVersions[currentVersion].apiVersions.includes(router.query.apiVersion)) || + false + + const currentDate = ( + isValidApiVersion ? router.query.apiVersion : allVersions[currentVersion].latestApiVersion + ) as string const currentDateDisplayText = currentDate === allVersions[currentVersion].latestApiVersion diff --git a/pages/[versionId]/rest/[category]/[subcategory].tsx b/pages/[versionId]/rest/[category]/[subcategory].tsx index 38551bf12b8b..3220f02ff470 100644 --- a/pages/[versionId]/rest/[category]/[subcategory].tsx +++ b/pages/[versionId]/rest/[category]/[subcategory].tsx @@ -39,8 +39,10 @@ export const getServerSideProps: GetServerSideProps = async (context) => const currentVersion = context.params!.versionId as string const currentLanguage = req.context.currentLanguage as string const allVersions = req.context.allVersions - const apiVersion = context.query.apiVersion || allVersions[currentVersion].latestApiVersion - + const queryApiVersion = context.query.apiVersion + const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion) + ? queryApiVersion + : allVersions[currentVersion].latestApiVersion // For pages with category level only operations like /rest/billing, we set // the subcategory's value to be the category for the call to getRest() if (!subCategory) { diff --git a/pages/[versionId]/rest/[category]/index.tsx b/pages/[versionId]/rest/[category]/index.tsx index fc381cf7ef55..d593c235069d 100644 --- a/pages/[versionId]/rest/[category]/index.tsx +++ b/pages/[versionId]/rest/[category]/index.tsx @@ -66,7 +66,10 @@ export const getServerSideProps: GetServerSideProps = async (context) => const currentVersion = context.params!.versionId as string const currentLanguage = req.context.currentLanguage as string const allVersions = req.context.allVersions - const apiVersion = context.query.apiVersion || allVersions[currentVersion].latestApiVersion + const queryApiVersion = context.query.apiVersion + const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion) + ? queryApiVersion + : allVersions[currentVersion].latestApiVersion // For pages with category level only operations like /rest/billing, we set // the subcategory's value to be the category for the call to getRest()