@inditextech/swagger-ui-plugin-diff-highlight 0.3.0-SNAPSHOT.111.1 → 0.3.0-SNAPSHOT.125.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -230,18 +230,20 @@ A modified attribute type maps to an `updated` schema property. Supply the old a
230
230
 
231
231
  ## How it works
232
232
 
233
- The plugin uses Swagger UI's [`wrapComponents`](https://swagger-ui.github.io/swagger-ui/usage/customization/plugin-api/) API to intercept rendering and attach diff CSS classes. It wraps four components (note the exact registration casing):
233
+ The plugin uses Swagger UI's [`wrapComponents`](https://swagger-ui.github.io/swagger-ui/usage/customization/plugin-api/) API to intercept rendering and attach diff CSS classes. It wraps five components (note the exact registration casing):
234
234
 
235
- | Component | What it marks | Where it reads the status |
236
- | -------------- | ----------------------- | ------------------------------------------------------ |
237
- | `operation` | An endpoint / opblock | The **unresolved** spec via `specSelectors`/`specPath` |
238
- | `parameterRow` | A request parameter row | `props.param` / `props.rawParam` |
239
- | `response` | A response row | `props.response` |
240
- | `Model` | A schema property | `props.schema` |
235
+ | Component | What it marks | Where it reads the status |
236
+ | --------------- | ----------------------------------------------------------- | ----------------------------------------------------------------------- |
237
+ | `operation` | An endpoint / opblock | The **unresolved** spec via `specSelectors`/`specPath` |
238
+ | `parameterRow` | A request parameter row | `props.param` / `props.rawParam` |
239
+ | `response` | A response row | `props.response` |
240
+ | `Model` | A schema property | `props.schema` |
241
+ | `ModelCollapse` | A schema accordion that contains a change (`diff-ancestor`) | The enclosing `Model`'s schema, or the unresolved spec for root schemas |
241
242
 
242
- Two implementation details worth knowing:
243
+ Three implementation details worth knowing:
243
244
 
244
245
  - **Operations are read from the unresolved spec.** Swagger UI resolves an operation's subtree _lazily_, only once it is expanded. The plugin reads `x-diff-status` straight from `specSelectors.specJson()` using the `specPath`, so the mark is visible whether the operation is **collapsed or expanded**.
246
+ - **Schema accordions show where changes hide.** A schema accordion whose subtree contains an added, deleted, or updated schema (following nested properties, items, compositions, and `$ref`s) gets the `diff-ancestor` class. Its header text turns ochre whether it is collapsed or expanded. For a nested schema, the property name turns ochre too. Leaf colors still win inside the header.
245
247
  - **Table layout is preserved.** Rows (`<tr>`) must stay direct children of their table, so the plugin wraps them in a `display: contents` element to apply the diff class without breaking the table flow.
246
248
 
247
249
  ### Notes / caveats
package/dist/diff.css CHANGED
@@ -3,25 +3,32 @@
3
3
 
4
4
  /* ----------------------------------------------------------------------------
5
5
  * Diff highlighting
6
+ *
7
+ * Swagger UI's own stylesheet colors most elements with selectors up to eight
8
+ * classes deep. The added / deleted / updated marks must win over all of
9
+ * them, so those selectors carry `:not(#_)`. It matches every element, since
10
+ * nothing has the id `_`, and adds ID-level specificity that no Swagger UI
11
+ * class selector reaches. Rules without it (updated old / new values and the
12
+ * ancestor mark) are meant to lose to the marks inside them.
6
13
  * ------------------------------------------------------------------------- */
7
14
 
8
15
  /* Added: green + bold */
9
- .diff-added,
10
- .diff-added * {
11
- color: #1b7e3c !important;
12
- font-weight: 700 !important;
16
+ .diff-added:not(#_),
17
+ .diff-added:not(#_) * {
18
+ color: #1b7e3c;
19
+ font-weight: 700;
13
20
  }
14
21
 
15
22
  /* Deleted: red + strikethrough */
16
- .diff-deleted,
17
- .diff-deleted * {
18
- color: #c0392b !important;
19
- text-decoration: line-through !important;
23
+ .diff-deleted:not(#_),
24
+ .diff-deleted:not(#_) * {
25
+ color: #c0392b;
26
+ text-decoration: line-through;
20
27
  }
21
28
 
22
29
  /* Updated rows (parameters / operations / responses): amber marker */
23
- .diff-wrap.diff-updated * {
24
- color: #b9770e !important;
30
+ .diff-wrap.diff-updated:not(#_) * {
31
+ color: #b9770e;
25
32
  }
26
33
 
27
34
  /* Updated attribute value: original (red strike) -> new (green bold) */
@@ -50,34 +57,53 @@
50
57
  * The value cell (2nd <td>) carries the diff class; :has() lets us reach the
51
58
  * sibling name cell (1st <td>) rendered by ObjectModel. The `> td >` scoping
52
59
  * keeps a change from leaking onto ancestor rows of nested objects. */
53
- .swagger-ui tr.property-row:has(> td > .diff-deleted) > td:first-child,
54
- .swagger-ui tr.property-row:has(> td > .diff-deleted) > td:first-child * {
55
- color: #c0392b !important;
56
- text-decoration: line-through !important;
60
+ .swagger-ui:not(#_) tr.property-row:has(> td > .diff-deleted) > td:first-child,
61
+ .swagger-ui:not(#_)
62
+ tr.property-row:has(> td > .diff-deleted)
63
+ > td:first-child
64
+ * {
65
+ color: #c0392b;
66
+ text-decoration: line-through;
67
+ }
68
+
69
+ .swagger-ui:not(#_) tr.property-row:has(> td > .diff-added) > td:first-child,
70
+ .swagger-ui:not(#_) tr.property-row:has(> td > .diff-added) > td:first-child * {
71
+ color: #1b7e3c;
72
+ font-weight: 700;
73
+ }
74
+
75
+ /* Schema accordion segments hiding a change: mark only the header button so
76
+ * the expanded body stays unpainted. No `:not(#_)`, so the added / deleted
77
+ * leaf rules above still win inside the header. */
78
+ .swagger-ui .diff-ancestor > .model-box-control,
79
+ .swagger-ui .diff-ancestor > .model-box-control * {
80
+ color: #b9770e;
57
81
  }
58
82
 
59
- .swagger-ui tr.property-row:has(> td > .diff-added) > td:first-child,
60
- .swagger-ui tr.property-row:has(> td > .diff-added) > td:first-child * {
61
- color: #1b7e3c !important;
62
- font-weight: 700 !important;
83
+ /* A nested schema's accordion has no title, so also mark the property name
84
+ * cell of the row whose value is that accordion. */
85
+ .swagger-ui
86
+ tr.property-row:has(> td > .model > .diff-ancestor)
87
+ > td:first-child {
88
+ color: #b9770e;
63
89
  }
64
90
 
65
91
  /* Operation-level changes: tag the whole opblock but only paint the
66
92
  * always-visible summary row, so the mark is shown whether the operation is
67
93
  * collapsed or expanded. */
68
- .diff-op-deleted .opblock-summary,
69
- .diff-op-deleted .opblock-summary * {
70
- color: #c0392b !important;
71
- text-decoration: line-through !important;
94
+ .diff-op-deleted:not(#_) .opblock-summary,
95
+ .diff-op-deleted:not(#_) .opblock-summary * {
96
+ color: #c0392b;
97
+ text-decoration: line-through;
72
98
  }
73
99
 
74
- .diff-op-added .opblock-summary,
75
- .diff-op-added .opblock-summary * {
76
- color: #1b7e3c !important;
77
- font-weight: 700 !important;
100
+ .diff-op-added:not(#_) .opblock-summary,
101
+ .diff-op-added:not(#_) .opblock-summary * {
102
+ color: #1b7e3c;
103
+ font-weight: 700;
78
104
  }
79
105
 
80
- .diff-op-updated .opblock-summary,
81
- .diff-op-updated .opblock-summary * {
82
- color: #b9770e !important;
106
+ .diff-op-updated:not(#_) .opblock-summary,
107
+ .diff-op-updated:not(#_) .opblock-summary * {
108
+ color: #b9770e;
83
109
  }
package/dist/index.js CHANGED
@@ -36,8 +36,61 @@ module.exports = __toCommonJS(index_exports);
36
36
 
37
37
  // src/DiffHighlightPlugin.jsx
38
38
  var import_react = __toESM(require("react"));
39
+ var EnumDiffContext = (0, import_react.createContext)(null);
40
+ var enumValueKey = (value) => JSON.stringify(
41
+ value,
42
+ (_, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(
43
+ Object.keys(item).sort((a, b) => a.localeCompare(b)).map((key) => [key, item[key]])
44
+ ) : item
45
+ );
39
46
  var wrapContents = (status, children) => /* @__PURE__ */ import_react.default.createElement("div", { className: `diff-wrap diff-${status}`, style: { display: "contents" } }, children);
40
47
  var readStatus = (immutableMap) => immutableMap && typeof immutableMap.get === "function" ? immutableMap.get("x-diff-status") : void 0;
48
+ var CHANGED_STATUSES = /* @__PURE__ */ new Set(["added", "deleted", "updated"]);
49
+ var isMapLike = (value) => value != null && typeof value.get === "function";
50
+ var valuesOf = (collection) => collection && typeof collection.valueSeq === "function" ? collection.valueSeq().toArray() : [];
51
+ var childSchemas = (schema) => [
52
+ ...valuesOf(schema.get("properties")),
53
+ schema.get("items"),
54
+ schema.get("additionalProperties"),
55
+ ...valuesOf(schema.get("allOf")),
56
+ ...valuesOf(schema.get("anyOf")),
57
+ ...valuesOf(schema.get("oneOf")),
58
+ schema.get("not")
59
+ ];
60
+ var containsChange = (schema, resolveRef = () => void 0) => {
61
+ const seenRefs = /* @__PURE__ */ new Set();
62
+ const deref = (node) => {
63
+ const ref = isMapLike(node) ? node.get("$ref") : void 0;
64
+ if (typeof ref !== "string") {
65
+ return node;
66
+ }
67
+ if (seenRefs.has(ref)) {
68
+ return void 0;
69
+ }
70
+ seenRefs.add(ref);
71
+ return resolveRef(ref);
72
+ };
73
+ const hasChangedDescendant = (node) => childSchemas(node).some((child) => {
74
+ const target = deref(child);
75
+ if (!isMapLike(target)) {
76
+ return false;
77
+ }
78
+ return CHANGED_STATUSES.has(readStatus(target)) || hasChangedDescendant(target);
79
+ });
80
+ const root = deref(schema);
81
+ if (!isMapLike(root)) {
82
+ return false;
83
+ }
84
+ return readStatus(root) === "updated" || hasChangedDescendant(root);
85
+ };
86
+ var localRefResolver = (specJson) => (ref) => ref.startsWith("#/") && isMapLike(specJson) ? specJson.getIn(
87
+ ref.slice(2).split("/").map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~"))
88
+ ) : void 0;
89
+ var specJsonOf = (system) => {
90
+ var _a, _b;
91
+ return (_b = (_a = system == null ? void 0 : system.specSelectors) == null ? void 0 : _a.specJson) == null ? void 0 : _b.call(_a);
92
+ };
93
+ var ContainsChangeContext = (0, import_react.createContext)(void 0);
41
94
  var describeSchema = (schema) => {
42
95
  if (!schema || typeof schema.get !== "function") {
43
96
  return "any";
@@ -54,20 +107,104 @@ var DiffHighlightPlugin = () => ({
54
107
  /**
55
108
  * Schema properties: each property value is rendered as a nested `<Model>`.
56
109
  */
57
- Model: (Original) => (props) => {
58
- const status = readStatus(props.schema);
59
- if (status === "deleted") {
60
- return /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-deleted" }, /* @__PURE__ */ import_react.default.createElement(Original, { ...props }));
110
+ Model: (Original, system) => function DiffModel(props) {
111
+ const { schema: rawSchema, getComponent } = props;
112
+ const status = readStatus(rawSchema);
113
+ const original = rawSchema == null ? void 0 : rawSchema.get("x-diff-original");
114
+ const updated = rawSchema == null ? void 0 : rawSchema.get("x-diff-new");
115
+ const originalEnum = original == null ? void 0 : original.get("enum");
116
+ const updatedEnum = updated == null ? void 0 : updated.get("enum");
117
+ const enumChanged = status === "updated" && Boolean(originalEnum || updatedEnum);
118
+ const type = (rawSchema == null ? void 0 : rawSchema.get("type")) || "object";
119
+ const supplemental = type === "object" || type === "array";
120
+ const enumDiff = (0, import_react.useMemo)(
121
+ () => enumChanged ? { original, updated, supplemental, getComponent } : null,
122
+ [enumChanged, original, updated, supplemental, getComponent]
123
+ );
124
+ if (status === "updated" && !enumChanged) {
125
+ const originalType = describeSchema(original);
126
+ const updatedType = describeSchema(updated);
127
+ return /* @__PURE__ */ import_react.default.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-inline" }, originalType === updatedType ? updatedType : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-old" }, originalType), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-new" }, updatedType))));
61
128
  }
62
- if (status === "added") {
63
- return /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-added" }, /* @__PURE__ */ import_react.default.createElement(Original, { ...props }));
129
+ const marked = containsChange(
130
+ rawSchema,
131
+ localRefResolver(specJsonOf(system))
132
+ );
133
+ if (enumChanged) {
134
+ const schema = supplemental ? rawSchema.remove("enum").remove("x-diff-status").remove("x-diff-original").remove("x-diff-new") : rawSchema.set("enum", updatedEnum || originalEnum);
135
+ return /* @__PURE__ */ import_react.default.createElement(ContainsChangeContext.Provider, { value: marked }, /* @__PURE__ */ import_react.default.createElement(EnumDiffContext.Provider, { value: enumDiff }, /* @__PURE__ */ import_react.default.createElement(Original, { ...props, schema })));
64
136
  }
65
- if (status === "updated") {
66
- const original = props.schema.get("x-diff-original");
67
- const updated = props.schema.get("x-diff-new");
68
- return /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-inline" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-old" }, describeSchema(original)), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-new" }, describeSchema(updated)));
137
+ const model = /* @__PURE__ */ import_react.default.createElement(ContainsChangeContext.Provider, { value: marked }, /* @__PURE__ */ import_react.default.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ import_react.default.createElement(Original, { ...props })));
138
+ if (status === "deleted" || status === "added") {
139
+ return /* @__PURE__ */ import_react.default.createElement("span", { className: `diff-${status}` }, model);
69
140
  }
70
- return /* @__PURE__ */ import_react.default.createElement(Original, { ...props });
141
+ return model;
142
+ },
143
+ /**
144
+ * Schema accordions: tag every segment whose subtree hides a change so its
145
+ * header can be marked while collapsed. Inside a `Model` the nearest
146
+ * Model's answer applies; the Schemas section's root accordions render
147
+ * outside any Model and before their subtree is resolved, so they read
148
+ * the raw spec at their `specPath`.
149
+ *
150
+ * An object or array whose enum changed also gets the enum diff appended
151
+ * to its accordion body.
152
+ */
153
+ ModelCollapse: (Original, system) => function DiffModelCollapse(props) {
154
+ var _a;
155
+ const fromModel = (0, import_react.useContext)(ContainsChangeContext);
156
+ const diff = (0, import_react.useContext)(EnumDiffContext);
157
+ const nestedDiff = (0, import_react.useMemo)(
158
+ () => diff && { ...diff, supplemental: false },
159
+ [diff]
160
+ );
161
+ const specJson = specJsonOf(system);
162
+ const marked = fromModel != null ? fromModel : ((_a = props.specPath) == null ? void 0 : _a.size) > 0 && isMapLike(specJson) && containsChange(
163
+ specJson.getIn(props.specPath),
164
+ localRefResolver(specJson)
165
+ );
166
+ const classes = marked ? [props.classes, "diff-ancestor"].filter(Boolean).join(" ") : props.classes;
167
+ if (!(diff == null ? void 0 : diff.supplemental)) {
168
+ return /* @__PURE__ */ import_react.default.createElement(Original, { ...props, classes });
169
+ }
170
+ const EnumModel = diff.getComponent("EnumModel");
171
+ return /* @__PURE__ */ import_react.default.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ import_react.default.createElement(Original, { ...props, classes }, props.children, /* @__PURE__ */ import_react.default.createElement(EnumDiffContext.Provider, { value: nestedDiff }, /* @__PURE__ */ import_react.default.createElement(EnumModel, { getComponent: diff.getComponent }))));
172
+ },
173
+ EnumModel: (Original) => function EnumModel(props) {
174
+ var _a, _b, _c, _d;
175
+ const diff = (0, import_react.useContext)(EnumDiffContext);
176
+ if (!diff) return /* @__PURE__ */ import_react.default.createElement(Original, { ...props });
177
+ const originalEnum = (_b = (_a = diff.original) == null ? void 0 : _a.get("enum")) == null ? void 0 : _b.toJS();
178
+ const updatedEnum = (_d = (_c = diff.updated) == null ? void 0 : _c.get("enum")) == null ? void 0 : _d.toJS();
179
+ const originalKeys = new Set(originalEnum == null ? void 0 : originalEnum.map(enumValueKey));
180
+ const updatedKeys = new Set(updatedEnum == null ? void 0 : updatedEnum.map(enumValueKey));
181
+ const values = (originalEnum || []).concat(
182
+ (updatedEnum == null ? void 0 : updatedEnum.filter(
183
+ (value) => !originalKeys.has(enumValueKey(value))
184
+ )) || []
185
+ );
186
+ const originalType = describeSchema(diff.original);
187
+ const updatedType = describeSchema(diff.updated);
188
+ const valueClass = (key) => {
189
+ if (!originalKeys.has(key)) return "diff-added";
190
+ if (!updatedKeys.has(key)) return "diff-deleted";
191
+ return void 0;
192
+ };
193
+ const ModelCollapse = props.getComponent("ModelCollapse");
194
+ return /* @__PURE__ */ import_react.default.createElement("span", { className: "prop-enum" }, /* @__PURE__ */ import_react.default.createElement("br", null), originalType !== updatedType && /* @__PURE__ */ import_react.default.createElement("div", { className: "diff-type-change" }, "Type:", " ", /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-inline" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-old" }, originalType), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-updated-new" }, updatedType))), "Enum:", /* @__PURE__ */ import_react.default.createElement("br", null), /* @__PURE__ */ import_react.default.createElement(
195
+ ModelCollapse,
196
+ {
197
+ collapsedContent: /* @__PURE__ */ import_react.default.createElement("span", null, "Array [ ", values.length, " ]")
198
+ },
199
+ !originalEnum && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-deleted" }, "unrestricted"), " "),
200
+ "[ ",
201
+ values.map((value, index) => {
202
+ const key = enumValueKey(value);
203
+ return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key }, index > 0 && ", ", /* @__PURE__ */ import_react.default.createElement("span", { className: valueClass(key) }, typeof value === "string" ? value : JSON.stringify(value)));
204
+ }),
205
+ " ]",
206
+ !updatedEnum && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, " ", /* @__PURE__ */ import_react.default.createElement("span", { className: "diff-added" }, "unrestricted"))
207
+ ));
71
208
  },
72
209
  /**
73
210
  * Parameters: one `<tr>` per parameter; status lives on the param object.
package/dist/index.mjs CHANGED
@@ -1,7 +1,60 @@
1
1
  // src/DiffHighlightPlugin.jsx
2
- import React from "react";
2
+ import React, { createContext, useContext, useMemo } from "react";
3
+ var EnumDiffContext = createContext(null);
4
+ var enumValueKey = (value) => JSON.stringify(
5
+ value,
6
+ (_, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(
7
+ Object.keys(item).sort((a, b) => a.localeCompare(b)).map((key) => [key, item[key]])
8
+ ) : item
9
+ );
3
10
  var wrapContents = (status, children) => /* @__PURE__ */ React.createElement("div", { className: `diff-wrap diff-${status}`, style: { display: "contents" } }, children);
4
11
  var readStatus = (immutableMap) => immutableMap && typeof immutableMap.get === "function" ? immutableMap.get("x-diff-status") : void 0;
12
+ var CHANGED_STATUSES = /* @__PURE__ */ new Set(["added", "deleted", "updated"]);
13
+ var isMapLike = (value) => value != null && typeof value.get === "function";
14
+ var valuesOf = (collection) => collection && typeof collection.valueSeq === "function" ? collection.valueSeq().toArray() : [];
15
+ var childSchemas = (schema) => [
16
+ ...valuesOf(schema.get("properties")),
17
+ schema.get("items"),
18
+ schema.get("additionalProperties"),
19
+ ...valuesOf(schema.get("allOf")),
20
+ ...valuesOf(schema.get("anyOf")),
21
+ ...valuesOf(schema.get("oneOf")),
22
+ schema.get("not")
23
+ ];
24
+ var containsChange = (schema, resolveRef = () => void 0) => {
25
+ const seenRefs = /* @__PURE__ */ new Set();
26
+ const deref = (node) => {
27
+ const ref = isMapLike(node) ? node.get("$ref") : void 0;
28
+ if (typeof ref !== "string") {
29
+ return node;
30
+ }
31
+ if (seenRefs.has(ref)) {
32
+ return void 0;
33
+ }
34
+ seenRefs.add(ref);
35
+ return resolveRef(ref);
36
+ };
37
+ const hasChangedDescendant = (node) => childSchemas(node).some((child) => {
38
+ const target = deref(child);
39
+ if (!isMapLike(target)) {
40
+ return false;
41
+ }
42
+ return CHANGED_STATUSES.has(readStatus(target)) || hasChangedDescendant(target);
43
+ });
44
+ const root = deref(schema);
45
+ if (!isMapLike(root)) {
46
+ return false;
47
+ }
48
+ return readStatus(root) === "updated" || hasChangedDescendant(root);
49
+ };
50
+ var localRefResolver = (specJson) => (ref) => ref.startsWith("#/") && isMapLike(specJson) ? specJson.getIn(
51
+ ref.slice(2).split("/").map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~"))
52
+ ) : void 0;
53
+ var specJsonOf = (system) => {
54
+ var _a, _b;
55
+ return (_b = (_a = system == null ? void 0 : system.specSelectors) == null ? void 0 : _a.specJson) == null ? void 0 : _b.call(_a);
56
+ };
57
+ var ContainsChangeContext = createContext(void 0);
5
58
  var describeSchema = (schema) => {
6
59
  if (!schema || typeof schema.get !== "function") {
7
60
  return "any";
@@ -18,20 +71,104 @@ var DiffHighlightPlugin = () => ({
18
71
  /**
19
72
  * Schema properties: each property value is rendered as a nested `<Model>`.
20
73
  */
21
- Model: (Original) => (props) => {
22
- const status = readStatus(props.schema);
23
- if (status === "deleted") {
24
- return /* @__PURE__ */ React.createElement("span", { className: "diff-deleted" }, /* @__PURE__ */ React.createElement(Original, { ...props }));
74
+ Model: (Original, system) => function DiffModel(props) {
75
+ const { schema: rawSchema, getComponent } = props;
76
+ const status = readStatus(rawSchema);
77
+ const original = rawSchema == null ? void 0 : rawSchema.get("x-diff-original");
78
+ const updated = rawSchema == null ? void 0 : rawSchema.get("x-diff-new");
79
+ const originalEnum = original == null ? void 0 : original.get("enum");
80
+ const updatedEnum = updated == null ? void 0 : updated.get("enum");
81
+ const enumChanged = status === "updated" && Boolean(originalEnum || updatedEnum);
82
+ const type = (rawSchema == null ? void 0 : rawSchema.get("type")) || "object";
83
+ const supplemental = type === "object" || type === "array";
84
+ const enumDiff = useMemo(
85
+ () => enumChanged ? { original, updated, supplemental, getComponent } : null,
86
+ [enumChanged, original, updated, supplemental, getComponent]
87
+ );
88
+ if (status === "updated" && !enumChanged) {
89
+ const originalType = describeSchema(original);
90
+ const updatedType = describeSchema(updated);
91
+ return /* @__PURE__ */ React.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ React.createElement("span", { className: "diff-updated-inline" }, originalType === updatedType ? updatedType : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("span", { className: "diff-updated-old" }, originalType), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-new" }, updatedType))));
25
92
  }
26
- if (status === "added") {
27
- return /* @__PURE__ */ React.createElement("span", { className: "diff-added" }, /* @__PURE__ */ React.createElement(Original, { ...props }));
93
+ const marked = containsChange(
94
+ rawSchema,
95
+ localRefResolver(specJsonOf(system))
96
+ );
97
+ if (enumChanged) {
98
+ const schema = supplemental ? rawSchema.remove("enum").remove("x-diff-status").remove("x-diff-original").remove("x-diff-new") : rawSchema.set("enum", updatedEnum || originalEnum);
99
+ return /* @__PURE__ */ React.createElement(ContainsChangeContext.Provider, { value: marked }, /* @__PURE__ */ React.createElement(EnumDiffContext.Provider, { value: enumDiff }, /* @__PURE__ */ React.createElement(Original, { ...props, schema })));
28
100
  }
29
- if (status === "updated") {
30
- const original = props.schema.get("x-diff-original");
31
- const updated = props.schema.get("x-diff-new");
32
- return /* @__PURE__ */ React.createElement("span", { className: "diff-updated-inline" }, /* @__PURE__ */ React.createElement("span", { className: "diff-updated-old" }, describeSchema(original)), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-new" }, describeSchema(updated)));
101
+ const model = /* @__PURE__ */ React.createElement(ContainsChangeContext.Provider, { value: marked }, /* @__PURE__ */ React.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ React.createElement(Original, { ...props })));
102
+ if (status === "deleted" || status === "added") {
103
+ return /* @__PURE__ */ React.createElement("span", { className: `diff-${status}` }, model);
33
104
  }
34
- return /* @__PURE__ */ React.createElement(Original, { ...props });
105
+ return model;
106
+ },
107
+ /**
108
+ * Schema accordions: tag every segment whose subtree hides a change so its
109
+ * header can be marked while collapsed. Inside a `Model` the nearest
110
+ * Model's answer applies; the Schemas section's root accordions render
111
+ * outside any Model and before their subtree is resolved, so they read
112
+ * the raw spec at their `specPath`.
113
+ *
114
+ * An object or array whose enum changed also gets the enum diff appended
115
+ * to its accordion body.
116
+ */
117
+ ModelCollapse: (Original, system) => function DiffModelCollapse(props) {
118
+ var _a;
119
+ const fromModel = useContext(ContainsChangeContext);
120
+ const diff = useContext(EnumDiffContext);
121
+ const nestedDiff = useMemo(
122
+ () => diff && { ...diff, supplemental: false },
123
+ [diff]
124
+ );
125
+ const specJson = specJsonOf(system);
126
+ const marked = fromModel != null ? fromModel : ((_a = props.specPath) == null ? void 0 : _a.size) > 0 && isMapLike(specJson) && containsChange(
127
+ specJson.getIn(props.specPath),
128
+ localRefResolver(specJson)
129
+ );
130
+ const classes = marked ? [props.classes, "diff-ancestor"].filter(Boolean).join(" ") : props.classes;
131
+ if (!(diff == null ? void 0 : diff.supplemental)) {
132
+ return /* @__PURE__ */ React.createElement(Original, { ...props, classes });
133
+ }
134
+ const EnumModel = diff.getComponent("EnumModel");
135
+ return /* @__PURE__ */ React.createElement(EnumDiffContext.Provider, { value: null }, /* @__PURE__ */ React.createElement(Original, { ...props, classes }, props.children, /* @__PURE__ */ React.createElement(EnumDiffContext.Provider, { value: nestedDiff }, /* @__PURE__ */ React.createElement(EnumModel, { getComponent: diff.getComponent }))));
136
+ },
137
+ EnumModel: (Original) => function EnumModel(props) {
138
+ var _a, _b, _c, _d;
139
+ const diff = useContext(EnumDiffContext);
140
+ if (!diff) return /* @__PURE__ */ React.createElement(Original, { ...props });
141
+ const originalEnum = (_b = (_a = diff.original) == null ? void 0 : _a.get("enum")) == null ? void 0 : _b.toJS();
142
+ const updatedEnum = (_d = (_c = diff.updated) == null ? void 0 : _c.get("enum")) == null ? void 0 : _d.toJS();
143
+ const originalKeys = new Set(originalEnum == null ? void 0 : originalEnum.map(enumValueKey));
144
+ const updatedKeys = new Set(updatedEnum == null ? void 0 : updatedEnum.map(enumValueKey));
145
+ const values = (originalEnum || []).concat(
146
+ (updatedEnum == null ? void 0 : updatedEnum.filter(
147
+ (value) => !originalKeys.has(enumValueKey(value))
148
+ )) || []
149
+ );
150
+ const originalType = describeSchema(diff.original);
151
+ const updatedType = describeSchema(diff.updated);
152
+ const valueClass = (key) => {
153
+ if (!originalKeys.has(key)) return "diff-added";
154
+ if (!updatedKeys.has(key)) return "diff-deleted";
155
+ return void 0;
156
+ };
157
+ const ModelCollapse = props.getComponent("ModelCollapse");
158
+ return /* @__PURE__ */ React.createElement("span", { className: "prop-enum" }, /* @__PURE__ */ React.createElement("br", null), originalType !== updatedType && /* @__PURE__ */ React.createElement("div", { className: "diff-type-change" }, "Type:", " ", /* @__PURE__ */ React.createElement("span", { className: "diff-updated-inline" }, /* @__PURE__ */ React.createElement("span", { className: "diff-updated-old" }, originalType), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-arrow" }, " \u2192 "), /* @__PURE__ */ React.createElement("span", { className: "diff-updated-new" }, updatedType))), "Enum:", /* @__PURE__ */ React.createElement("br", null), /* @__PURE__ */ React.createElement(
159
+ ModelCollapse,
160
+ {
161
+ collapsedContent: /* @__PURE__ */ React.createElement("span", null, "Array [ ", values.length, " ]")
162
+ },
163
+ !originalEnum && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("span", { className: "diff-deleted" }, "unrestricted"), " "),
164
+ "[ ",
165
+ values.map((value, index) => {
166
+ const key = enumValueKey(value);
167
+ return /* @__PURE__ */ React.createElement(React.Fragment, { key }, index > 0 && ", ", /* @__PURE__ */ React.createElement("span", { className: valueClass(key) }, typeof value === "string" ? value : JSON.stringify(value)));
168
+ }),
169
+ " ]",
170
+ !updatedEnum && /* @__PURE__ */ React.createElement(React.Fragment, null, " ", /* @__PURE__ */ React.createElement("span", { className: "diff-added" }, "unrestricted"))
171
+ ));
35
172
  },
36
173
  /**
37
174
  * Parameters: one `<tr>` per parameter; status lives on the param object.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inditextech/swagger-ui-plugin-diff-highlight",
3
- "version": "0.3.0-SNAPSHOT.111.1",
3
+ "version": "0.3.0-SNAPSHOT.125.1",
4
4
  "description": "Highlight API changes (added/updated/deleted) inline in swagger-ui-react via a custom diff-highlighting plugin.",
5
5
  "keywords": [
6
6
  "swagger",
@@ -74,6 +74,7 @@
74
74
  "@eslint/js": "^9.39.4",
75
75
  "@testing-library/jest-dom": "^6.6.3",
76
76
  "@testing-library/react": "^16.1.0",
77
+ "@vitest/coverage-v8": "^5.0.1",
77
78
  "eslint": "^9.39.4",
78
79
  "eslint-config-prettier": "^10.1.8",
79
80
  "eslint-plugin-react": "^7.37.5",