Skip to content

Commit

Permalink
fix(api-gen): overrides when deleting non existing item (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick authored Oct 17, 2024
1 parent 16634d3 commit 221af3c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 70 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-schools-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/api-gen": patch
---

Fix patching schema when there is an oveerite with the `_DELETE` key, and the value was not present in the original schema. In that case there is nothing to delete and value should be omitted.
7 changes: 3 additions & 4 deletions packages/api-gen/src/jsonOverrideUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import json5 from "json5";
import { validationRules } from "./validation-rules";
import { ofetch } from "ofetch";
import { OverridesSchema } from "./patchJsonSchema";
import jsonParser from "./parser/jsonParser";

export type ApiGenConfig = {
rules: Array<keyof typeof validationRules>;
Expand Down Expand Up @@ -65,14 +64,14 @@ export async function loadJsonOverrides({
if (isURL(pathToResolve)) {
const response = await ofetch(pathToResolve, {
responseType: "json",
parseResponse: jsonParser,
parseResponse: json5.parse,
});
return response;
} else {
const jsonOverridesFile = await readFileSync(pathToResolve, {
encoding: "utf-8",
});
const content = jsonParser(jsonOverridesFile);
const content = json5.parse(jsonOverridesFile);
return content;
}
} catch (error) {
Expand Down Expand Up @@ -135,7 +134,7 @@ export function displayPatchingSummary({
const formatColor = todosToFix.length ? c.yellow : c.green;
console.log(
formatColor(
`We've found ${c.bold(todosToFix.length)} warning(s) in the schema. Apply patches to fix them in the original schema.`,
`We've found ${c.bold(todosToFix.length)} warning(s) in the schema.${todosToFix.length ? " Apply patches to fix them in the original schema." : ""}`,
),
);
}
25 changes: 0 additions & 25 deletions packages/api-gen/src/parser/jsonParser.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/api-gen/src/parser/jsonParser.ts

This file was deleted.

24 changes: 0 additions & 24 deletions packages/api-gen/src/parser/parserPrescriber.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/api-gen/src/parser/parserPrescriber.ts

This file was deleted.

64 changes: 64 additions & 0 deletions packages/api-gen/src/patchJsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,70 @@ describe("patchJsonSchema", () => {
`);
});

it("should skip removing property from component if it's not there", async () => {
const { patchedSchema } = patchJsonSchema({
openApiSchema: json5.parse(`{
components: {
schemas: {
"ProductMedia": {
"description": "Added since version: 6.0.0.0",
"required": [],
"properties": {
"media": { "$ref": "#/components/schemas/Media" },
},
"type": "object"
},
},
},
}`),
jsonOverrides: json5.parse(`{
components: {
"ProductMedia": [
{
"properties": {
"thumbnails": {
"$ref": "_DELETE_",
"type": "array",
"items": { "$ref": "#/components/schemas/MediaThumbnail" }
}
}
},
{
"required": ["media"]
}
],
},
}`),
});

expect(patchedSchema).toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"ProductMedia": {
"description": "Added since version: 6.0.0.0",
"properties": {
"media": {
"$ref": "#/components/schemas/Media",
},
"thumbnails": {
"items": {
"$ref": "#/components/schemas/MediaThumbnail",
},
"type": "array",
},
},
"required": [
"media",
],
"type": "object",
},
},
},
}
`);
});

it("should add new component if there was none before", async () => {
const { patchedSchema } = patchJsonSchema({
openApiSchema: json5.parse(`{
Expand Down
14 changes: 14 additions & 0 deletions packages/api-gen/src/patchJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const extendedDefu = createDefu((obj, key, value) => {
return true;
}

// if there is no key in object, add it
if (!obj[key]) {
obj[key] = value;
}

// Feature to delete key from object
if (value === "_DELETE_") {
delete obj[key];
Expand All @@ -43,6 +48,10 @@ export function patchJsonSchema({
const outdatedPatches: string[][] = [];
let appliedPatches: number = 0;
const todosToFix: string[][] = [];
const schemaPaths: Array<{
path: string;
method: string;
}> = [];

Object.entries(openApiSchema.components?.schemas || {}).forEach((schema) => {
if (jsonOverrides?.components?.[schema[0]]) {
Expand Down Expand Up @@ -100,6 +109,10 @@ export function patchJsonSchema({
const pathName = pathObject[0];
Object.entries(pathObject[1]).forEach((singlePath) => {
const httpMethod = singlePath[0];
schemaPaths.push({
path: pathName,
method: httpMethod.toUpperCase(),
});

if (jsonOverrides?.paths?.[pathName]?.[httpMethod]) {
const overridePatches = jsonOverrides?.paths[pathName][httpMethod];
Expand Down Expand Up @@ -159,5 +172,6 @@ export function patchJsonSchema({
todosToFix,
appliedPatches,
outdatedPatches,
schemaPaths,
};
}

0 comments on commit 221af3c

Please sign in to comment.