Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim committed Dec 11, 2023
1 parent 7e665fd commit 9cdce16
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "openapi-workspaces",
"license": "MIT",
"private": true,
"version": "0.53.10-1",
"version": "0.53.10-2",
"workspaces": [
"projects/json-pointer-helpers",
"projects/openapi-io",
Expand Down
2 changes: 1 addition & 1 deletion projects/fastify-capture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/fastify-capture",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/json-pointer-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/json-pointer-helpers",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-io",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-utilities",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/optic",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/rulesets-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/rulesets-base",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/standard-rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/standard-rulesets",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.10-1",
"version": "0.53.10-2",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`can reduce size of operations by removing nested schemas 1`] = `undefined`;

exports[`can reduce size of responses by deleting descriptions + examples 1`] = `
{
"content": {
"application/json": {
"schema": {
"properties": {
"description": {
"type": "string",
},
"example": {
"type": "string",
},
},
"type": "object",
},
},
},
"description": "The response",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { test, expect } from '@jest/globals';
import { prepareOperation, prepareResponse } from '../prepare-openapi';

test('can reduce size of operations by removing nested schemas ', () => {
const output = prepareOperation({
description: 'delete all the orders for this user',
parameters: [
{
name: 'a',
in: 'query',
description: '123',
example: '123',
},
],
requestBody: {
description: 'what to send',
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
description: {
type: 'string',
},
orders: {
type: 'array',
description: 'item-ids',
example: ['1', '2', '3'],
items: {
type: 'string',
description: 'itemIds',
},
},
},
},
},
},
},
responses: {
'200': {
description: 'The response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
description: {
type: 'string',
description: 'Delete me',
},
},
},
},
},
},
},
});
expect(output).toMatchSnapshot();
});

test('can reduce size of responses by deleting descriptions + examples ', () => {
const output = prepareResponse({
description: 'The response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
description: {
type: 'string',
description: 'Delete me',
},
example: {
type: 'string',
example: '123',
},
},
},
},
},
});
expect(output).toMatchSnapshot();
});
54 changes: 51 additions & 3 deletions projects/standard-rulesets/src/lintgpt/prepare-openapi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jsonPointerHelpers } from '@useoptic/json-pointer-helpers';
import { OpenAPIV3 } from 'openapi-types';

export function prepareOperation(operation: OpenAPIV3.OperationObject) {
Expand All @@ -24,11 +25,58 @@ function removeSchemasFromOperation(operation: OpenAPIV3.OperationObject) {
}
}

function removeExamplesAndDescriptionsFromProperties(
value: any,
path: string[]
) {
if (Array.isArray(value)) {
value.forEach((item, index) =>
removeExamplesAndDescriptionsFromProperties(item, [
...path,
index.toString(),
])
);
} else if (typeof value === 'object' && value !== null) {
if (
value.hasOwnProperty('description') &&
jsonPointerHelpers.endsWith(
jsonPointerHelpers.compile([...path, 'description']),
['properties', '**', 'description']
)
) {
delete value['description'];
}

if (
value.hasOwnProperty('example') &&
jsonPointerHelpers.endsWith(
jsonPointerHelpers.compile([...path, 'example']),
['properties', '**', 'example']
)
) {
delete value['example'];
}

if (
value.hasOwnProperty('examples') &&
jsonPointerHelpers.endsWith(
jsonPointerHelpers.compile([...path, 'examples']),
['properties', '**', 'examples']
)
) {
delete value['examples'];
}

Object.keys(value).forEach((key) =>
removeExamplesAndDescriptionsFromProperties(value[key], [...path, key])
);
} else {
}
}

export function prepareResponse(response: OpenAPIV3.ResponseObject) {
const copied: OpenAPIV3.ResponseObject = JSON.parse(JSON.stringify(response));

for (const value of Object.values(copied.content ?? {})) {
value.schema = undefined;
}
removeExamplesAndDescriptionsFromProperties(copied, []);
return copied;
}

0 comments on commit 9cdce16

Please sign in to comment.