Skip to content

Commit

Permalink
Merge pull request openshift#6227 from spadgett/swagger-caching
Browse files Browse the repository at this point in the history
Bug 1866114: Don't store OpenAPI definitions in localStorage
  • Loading branch information
openshift-merge-robot authored Aug 9, 2020
2 parents d9d48f4 + 6daa119 commit c084c58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'monaco-languageclient/lib/monaco-converter';
import { getLanguageService, TextDocument } from 'yaml-language-server';
import { openAPItoJSONSchema } from '@console/internal/module/k8s/openapi-to-json-schema';
import { getStoredSwagger } from '@console/internal/module/k8s/swagger';
import { getSwaggerDefinitions } from '@console/internal/module/k8s/swagger';
import { global_BackgroundColor_dark_100 as editorBackground } from '@patternfly/react-tokens';

window.monaco.editor.defineTheme('console', {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const createYAMLService = () => {
const yamlService = getLanguageService(resolveSchema, workspaceContext, []);

// Prepare the schema
const yamlOpenAPI = getStoredSwagger();
const yamlOpenAPI = getSwaggerDefinitions();

// Convert the openAPI schema to something the language server understands
const kubernetesJSONSchema = openAPItoJSONSchema(yamlOpenAPI);
Expand Down
4 changes: 2 additions & 2 deletions frontend/public/components/sidebars/explore-type-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Breadcrumb, BreadcrumbItem, Button } from '@patternfly/react-core';

import {
getDefinitionKey,
getStoredSwagger,
getSwaggerDefinitions,
getSwaggerPath,
K8sKind,
SwaggerDefinition,
Expand All @@ -29,7 +29,7 @@ export const ExploreType: React.FC<ExploreTypeProps> = (props) => {
return null;
}

const allDefinitions: SwaggerDefinitions = getStoredSwagger();
const allDefinitions: SwaggerDefinitions = getSwaggerDefinitions();
if (!allDefinitions) {
return null;
}
Expand Down
58 changes: 17 additions & 41 deletions frontend/public/module/k8s/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,22 @@ export const getDefinitionKey = _.memoize(
referenceForModel,
);

// Cache parsed swagger to avoid reparsing the JSON each call.
let swagger: SwaggerDefinitions;
export const getStoredSwagger = (): SwaggerDefinitions => {
if (swagger) {
return swagger;
}
const json = window.localStorage.getItem(SWAGGER_LOCAL_STORAGE_KEY);
if (!json) {
return null;
}
try {
swagger = JSON.parse(json);
return swagger;
} catch (e) {
// eslint-disable-next-line no-console
console.error('Could not parse swagger JSON.', e);
return null;
}
};

const storeSwagger = (definitions: SwaggerDefinitions) => {
// Only store definitions to reduce the document size.
const json = JSON.stringify(definitions);
window.localStorage.setItem(SWAGGER_LOCAL_STORAGE_KEY, json);
swagger = definitions;
};
let swaggerDefinitions: SwaggerDefinitions;
export const getSwaggerDefinitions = (): SwaggerDefinitions => swaggerDefinitions;

export const fetchSwagger = async (): Promise<SwaggerDefinitions> => {
// Remove any old definitions from `localSotrage`. We rely on the browser cache now.
// TODO: We should be able to remove this in a future release.
localStorage.removeItem(SWAGGER_LOCAL_STORAGE_KEY);
try {
const response: SwaggerAPISpec = await coFetchJSON('api/kubernetes/openapi/v2');
if (!response.definitions) {
// eslint-disable-next-line no-console
console.error('Definitions missing in OpenAPI response.');
return null;
}
storeSwagger(response.definitions);
return response.definitions;
swaggerDefinitions = response.definitions;
return swaggerDefinitions;
} catch (e) {
// eslint-disable-next-line no-console
console.error('Could not get OpenAPI definitions', e);
Expand All @@ -66,12 +45,11 @@ export const fetchSwagger = async (): Promise<SwaggerDefinitions> => {
};

export const definitionFor = _.memoize((model: K8sKind): SwaggerDefinition => {
const allDefinitions: SwaggerDefinitions = getStoredSwagger();
if (!allDefinitions) {
if (!swaggerDefinitions) {
return null;
}
const key = getDefinitionKey(model, allDefinitions);
return _.get(allDefinitions, key);
const key = getDefinitionKey(model, swaggerDefinitions);
return _.get(swaggerDefinitions, key);
}, referenceForModel);

const getRef = (definition: SwaggerDefinition): string => {
Expand Down Expand Up @@ -102,25 +80,24 @@ export const getSwaggerPath = (
};

const findDefinition = (kindObj: K8sKind, propertyPath: string[]): SwaggerDefinition => {
const allDefinitions: SwaggerDefinitions = getStoredSwagger();
if (!allDefinitions) {
if (!swaggerDefinitions) {
return null;
}

const rootPath = getDefinitionKey(kindObj, allDefinitions);
const rootPath = getDefinitionKey(kindObj, swaggerDefinitions);
const path = propertyPath.reduce(
(currentPath: string[], nextProperty: string, i: number): string[] => {
if (!currentPath) {
return null;
}
// Don't follow the last reference since the description is not as good.
const followRef = i !== propertyPath.length - 1;
return getSwaggerPath(allDefinitions, currentPath, nextProperty, followRef);
return getSwaggerPath(swaggerDefinitions, currentPath, nextProperty, followRef);
},
[rootPath],
);

return path ? (_.get(allDefinitions, path) as SwaggerDefinition) : null;
return path ? (_.get(swaggerDefinitions, path) as SwaggerDefinition) : null;
};

export const getPropertyDescription = (
Expand All @@ -133,12 +110,11 @@ export const getPropertyDescription = (
};

export const getResourceDescription = _.memoize((kindObj: K8sKind): string => {
const allDefinitions: SwaggerDefinitions = getStoredSwagger();
if (!allDefinitions) {
if (!swaggerDefinitions) {
return null;
}
const key = getDefinitionKey(kindObj, allDefinitions);
return _.get(allDefinitions, [key, 'description']);
const key = getDefinitionKey(kindObj, swaggerDefinitions);
return _.get(swaggerDefinitions, [key, 'description']);
}, referenceForModel);

export type SwaggerDefinition = {
Expand Down

0 comments on commit c084c58

Please sign in to comment.