Skip to content

Commit

Permalink
fix(api-gen): error messages and merging patches (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick authored Nov 13, 2024
1 parent af0c307 commit 11ea41f
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-walls-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/api-gen": patch
---

Improved error messages and merging override patches.
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/b2b-quote-management/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/express-checkout/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/mollie-credit-card/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/api-types/adminApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10805,7 +10805,7 @@ export type Schemas = {
};
MultiNotFilter: {
/** @enum {string} */
operator: "AND" | "and" | "OR" | "or";
operator: "AND" | "OR" | "and" | "or";
queries: components["schemas"]["Filters"];
/** @enum {string} */
type: "multi" | "not";
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/api-gen/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export async function generate(args: {
type GenericRecord = never | null | string | string[] | number | { [key: string]: GenericRecord };
`,
transform(schemaObject, metadata) {
if (!schemaObject) {
throw new Error("Schema object is empty at " + metadata.path);
}
/**
* Add proper `translated` types for object fields without entity fields like id, createdAt, updatedAt etc.
*/
Expand Down
58 changes: 58 additions & 0 deletions packages/api-gen/src/mergingObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,62 @@ describe("mergingObjects - extendedDefu", () => {
}
`);
});

it("should omit _DELETE_ if object not existed", () => {
const result = extendedDefu(
{},
{
CartError: {
required: ["key", "level", "message", "messageKey"],
properties: {
items: "_DELETE_",
key: { type: "string" },
level: {
type: "number",
enum: [0, 10, 20],
description: "* `0` - notice,\n* `10` - warning,\n* `20` - error",
},
message: { type: "string" },
messageKey: { type: "string" },
},
},
},
);

expect(result).toMatchInlineSnapshot(`
{
"CartError": {
"properties": {
"items": "_DELETE_",
"key": {
"type": "string",
},
"level": {
"description": "* \`0\` - notice,
* \`10\` - warning,
* \`20\` - error",
"enum": [
0,
10,
20,
],
"type": "number",
},
"message": {
"type": "string",
},
"messageKey": {
"type": "string",
},
},
"required": [
"key",
"level",
"message",
"messageKey",
],
},
}
`);
});
});
134 changes: 134 additions & 0 deletions packages/api-gen/src/patchJsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,138 @@ describe("patchJsonSchema", () => {
expect(outdatedPatches.length).toBe(0);
});
});

it("should ignore _DELETE_ from overrides if additional", async () => {
const { patchedSchema } = patchJsonSchema({
openApiSchema: json5.parse(`{
"components": {
"schemas": {}
}
}`),
jsonOverrides: json5.parse(`{
"components": {
"CartError": [
{
"required": ["key", "level", "message", "messageKey"],
"properties": {
"items": "_DELETE_",
"key": { "type": "string" },
"level": {
"type": "number",
"enum": [0, 10, 20],
},
"message": { "type": "string" },
"messageKey": { "type": "string" }
}
}
],
}
}`),
});

expect(patchedSchema).toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"CartError": {
"properties": {
"key": {
"type": "string",
},
"level": {
"enum": [
0,
10,
20,
],
"type": "number",
},
"message": {
"type": "string",
},
"messageKey": {
"type": "string",
},
},
"required": [
"key",
"level",
"message",
"messageKey",
],
},
},
},
}
`);
});

it("should ignore _DELETE_ from overrides when patch", async () => {
const { patchedSchema } = patchJsonSchema({
openApiSchema: json5.parse(`{
"components": {
"schemas": {}
}
}`),
jsonOverrides: json5.parse(`{
"components": {
"CartError": [
{
"required": ["key", "level", "message", "messageKey"],
"properties": {
"key": { "type": "string" },
"level": {
"type": "number",
"enum": [0, 10, 20],
},
"message": { "type": "string" },
"messageKey": { "type": "string" }
}
},
{
"properties": {
"items": "_DELETE_"
}
}
],
}
}`),
});

expect(patchedSchema).toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"CartError": {
"properties": {
"key": {
"type": "string",
},
"level": {
"enum": [
0,
10,
20,
],
"type": "number",
},
"message": {
"type": "string",
},
"messageKey": {
"type": "string",
},
},
"required": [
"key",
"level",
"message",
"messageKey",
],
},
},
},
}
`);
});
});
7 changes: 5 additions & 2 deletions packages/api-gen/src/patchJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const extendedDefu = createDefu((obj, key, value) => {
if (Array.isArray(obj[key]) && Array.isArray(value)) {
// concat arrays but skip duplicates
// @ts-expect-error - we know that obj[key] is an array as we're inside this if statement
obj[key] = [...new Set([...obj[key], ...value])];
obj[key] = [...new Set([...obj[key], ...value])].sort();
return true;
}

Expand Down Expand Up @@ -99,7 +99,10 @@ export function patchJsonSchema({
if (!openApiSchema.components?.schemas?.[schema[0]]) {
const patches = Array.isArray(schema[1]) ? schema[1] : [schema[1]];
patches.forEach((patch: JSON) => {
patchedSchema.components.schemas[schema[0]] = patch;
patchedSchema.components.schemas[schema[0]] = extendedDefu(
patch,
patchedSchema.components.schemas[schema[0]],
);
appliedPatches++;
});
}
Expand Down
8 changes: 7 additions & 1 deletion packages/api-gen/src/processAstSchemaAndOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ export async function processAstSchemaAndOverrides(
const existingTypes = opExistingTypes;

const filePath = join("api-types", `${type}ApiTypes.d.ts`);
generateFile(filePath, operationsMap, existingTypes, componentsMap, options);
await generateFile(
filePath,
operationsMap,
existingTypes,
componentsMap,
options,
);
}
2 changes: 1 addition & 1 deletion templates/vue-demo-store/api-types/storeApiTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export type Schemas = {
keywords?: string;
linkNewTab?: boolean;
/** @enum {string} */
linkType?: "external" | "category" | "product" | "landing_page";
linkType?: "category" | "external" | "landing_page" | "product";
metaDescription?: string;
metaTitle?: string;
name: string;
Expand Down

0 comments on commit 11ea41f

Please sign in to comment.