Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(onshape): find BOM columns by property name, ignoring label case (#2019)

BettrCallRaulfaf739dd22af
diff --git a/.claude/rules/onshape-panel.md b/.claude/rules/onshape-panel.md
index 32484a7aac..3cc96c004d 100644
--- a/.claude/rules/onshape-panel.md
+++ b/.claude/rules/onshape-panel.md
@@ -122,6 +122,7 @@ The gateway rejects a request line over 4,096 bytes, and a PostgREST `.in()` lis
## Onshape API facts
- Root identity comes from element metadata ("Part number", "Name", "Description"); the BOM's top-level row (`includeTopLevelAssemblyRow=true`) is the fallback.
+- A company BOM template can relabel columns ("PART NUMBER", "Qty") but never their `propertyName` (`item`, `quantity`, `partNumber`, `revision`, `name`, `description`, `bomBehavior`). `parseBomTree` finds each column by `propertyName`, then by exact label, then by label ignoring case (`headerIdFor`, `panel/bom.ts`; a repeated label keeps its last header); the reserved and fixed property-name sets compare in lower case.
- A Part Studio model export needs exactly one `partId`. Thumbnails come from Carbon's model optimizer, never Onshape's thumbnail endpoint (it cannot select a part or configuration, and the optimizer renders one anyway).
- `GET /documents/d/{did}/w/{wid}/currentmicroversion` changes on any edit to the document. An assembly's own element microversion does not change when a Part Studio it uses does, so the export skip keys on the document's.
- `onshape.model.translation.complete` can be registered per document (`documentId`, `isTransient: true`); its payload carries `translationId`. A webhook that answers errors is dropped by Onshape.
diff --git a/packages/ee/src/onshape/panel/Panel.tsx b/packages/ee/src/onshape/panel/Panel.tsx
index 6b5d374d68..74144180b3 100644
--- a/packages/ee/src/onshape/panel/Panel.tsx
+++ b/packages/ee/src/onshape/panel/Panel.tsx
@@ -278,11 +278,12 @@ type PanelFieldsState =
};
// Every push reads these four itself: the Fields page shows them as fixed rows, never as a mapping.
+// Lower case, since a template can relabel them in any case.
const FIXED_PROPERTY_NAMES = new Set([
- "Part number",
- "Revision",
- "Name",
- "Description"
+ "part number",
+ "revision",
+ "name",
+ "description"
]);
// hasOwn: MAPPABLE_VALUE_TYPES is a plain object, so "constructor" would resolve to Object.prototype's.
@@ -2736,7 +2737,8 @@ function FieldsSection({
const offered = data.properties
.filter(
(property) =>
- property.mappable && !FIXED_PROPERTY_NAMES.has(property.name)
+ property.mappable &&
+ !FIXED_PROPERTY_NAMES.has(property.name.trim().toLowerCase())
)
.sort((a, b) => a.name.localeCompare(b.name));
// Item fields with a row of their own are set in that row.
diff --git a/packages/ee/src/onshape/panel/bom.test.ts b/packages/ee/src/onshape/panel/bom.test.ts
index 6dac2489b2..ca330ff2aa 100644
--- a/packages/ee/src/onshape/panel/bom.test.ts
+++ b/packages/ee/src/onshape/panel/bom.test.ts
@@ -241,6 +241,80 @@ describe("parseBomTree", () => {
parseBomTree({ headers: H, rows: [row("1", "A-1", 1)] }).missingColumns
).toEqual([]);
});
+
+ it("reads a template that relabels its columns, by Onshape's property name", () => {
+ const { lines, missingColumns } = parseBomTree({
+ headers: [
+ { id: "h-item", name: "Pos.", propertyName: "item" },
+ { id: "h-qty", name: "Qty", propertyName: "quantity" },
+ { id: "h-pn", name: "PART NUMBER", propertyName: "partNumber" },
+ { id: "h-rev", name: "REVISION", propertyName: "revision" },
+ { id: "h-name", name: "Benennung", propertyName: "name" },
+ { id: "h-pl", name: "Purchasing Level", propertyName: "h-pl" }
+ ],
+ rows: [row("1", "A-1", 2), row("1.1", "B-1", 3)]
+ });
+ expect(missingColumns).toEqual([]);
+ expect(lines).toHaveLength(1);
+ expect(lines[0]).toMatchObject({
+ partNumber: "A-1",
+ revision: "A",
+ name: "Name 1",
+ quantity: 2
+ });
+ expect(lines[0]!.children[0]).toMatchObject({
+ partNumber: "B-1",
+ quantity: 3
+ });
+ });
+
+ it("matches a label ignoring case when Onshape sends no property name", () => {
+ const { lines, missingColumns } = parseBomTree({
+ headers: H.map((header) => ({
+ ...header,
+ name: header.name.toUpperCase()
+ })),
+ rows: [row("1", "A-1", 4)]
+ });
+ expect(missingColumns).toEqual([]);
+ expect(lines[0]).toMatchObject({ partNumber: "A-1", quantity: 4 });
+ });
+
+ it("prefers an exact label over one that differs only in case", () => {
+ const { lines } = parseBomTree({
+ headers: [...H, { id: "h-custom", name: "PART NUMBER" }],
+ rows: [
+ {
+ ...row("1", "A-1", 1),
+ headerIdToValue: {
+ ...row("1", "A-1", 1).headerIdToValue,
+ "h-custom": "LEGACY-1"
+ }
+ }
+ ]
+ });
+ expect(lines[0]!.partNumber).toBe("A-1");
+ });
+
+ it("prefers Onshape's part number over a custom column with the same label", () => {
+ const { lines } = parseBomTree({
+ headers: [
+ ...H.filter((header) => header.id !== "h-pn"),
+ { id: "h-custom", name: "Part number", propertyName: "h-custom" },
+ { id: "h-pn", name: "PART NUMBER", propertyName: "partNumber" }
+ ],
+ rows: [
+ {
+ ...row("1", "A-1", 1),
+ headerIdToValue: {
+ ...row("1", "A-1", 1).headerIdToValue,
+ "h-custom": "LEGACY-1"
+ }
+ }
+ ]
+ });
+ expect(lines[0]!.partNumber).toBe("A-1");
+ });
});
describe("parseBomTree Subassembly BOM behavior", () => {
diff --git a/packages/ee/src/onshape/panel/bom.ts b/packages/ee/src/onshape/panel/bom.ts
index e3d353076a..3162f36554 100644
--- a/packages/ee/src/onshape/panel/bom.ts
+++ b/packages/ee/src/onshape/panel/bom.ts
@@ -3,8 +3,9 @@
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
/**
- * Onshape indented BOM → typed tree. Columns are addressed by display name;
- * nesting comes from the dotted "Item" index ("1.1.2"). Non-empty cells are
+ * Onshape indented BOM → typed tree. Columns are addressed by Onshape's
+ * property name, then by display name (`headerIdFor`); nesting
+ * comes from the dotted "Item" index ("1.1.2"). Non-empty cells are
* kept as properties keyed by column id, so a row resolves through the
* company's property map like a part.
*/
@@ -37,7 +38,12 @@ export type OnshapeBomNode = {
children: OnshapeBomNode[];
};
-type BomHeader = { id: string; name: string; valueType?: unknown };
+type BomHeader = {
+ id: string;
+ name: string;
+ propertyName?: unknown;
+ valueType?: unknown;
+};
type BomRow = {
headerIdToValue?: Record<string, unknown>;
itemSource?: Record<string, unknown>;
@@ -70,6 +76,50 @@ export const BOM_REQUIRED_COLUMNS = [
"Quantity"
] as const;
+/**
+ * Onshape's property name for each column the parser reads. A BOM template
+ * can relabel a column ("PART NUMBER", "Qty") but never renames its property.
+ */
+const COLUMN_PROPERTY_NAMES: Record<string, string> = {
+ Item: "item",
+ "Part number": "partNumber",
+ Quantity: "quantity",
+ Revision: "revision",
+ Name: "name",
+ Description: "description",
+ "Subassembly BOM behavior": "bomBehavior"
+};
+
+const labelKey = (label: string) => label.trim().toLowerCase();
+
+/**
+ * The column id for a label: by property name, then by the exact label (the
+ * lookup this replaced), then by label ignoring case. A repeated label keeps
+ * its last header, as that lookup did.
+ */
+function headerIdFor(headers: BomHeader[]) {
+ const byPropertyName = new Map<string, string>();
+ const byExactLabel = new Map<string, string>();
+ const byLabel = new Map<string, string>();
+ for (const header of headers) {
+ if (typeof header.propertyName === "string") {
+ byPropertyName.set(header.propertyName, header.id);
+ }
+ if (typeof header.name === "string") {
+ byExactLabel.set(header.name, header.id);
+ byLabel.set(labelKey(header.name), header.id);
+ }
+ }
+ return (label: string) => {
+ const propertyName = COLUMN_PROPERTY_NAMES[label];
+ return (
+ (propertyName ? byPropertyName.get(propertyName) : undefined) ??
+ byExactLabel.get(label) ??
+ byLabel.get(labelKey(label))
+ );
+ };
+}
+
export function parseBomTree(payload: unknown): {
/** The assembly's own row (index "0"), when the export included it. */
root: OnshapeBomNode | null;
@@ -84,13 +134,13 @@ export function parseBomTree(payload: unknown): {
const headers = Array.isArray(bom.headers) ? bom.headers : [];
const rows = Array.isArray(bom.rows) ? bom.rows : [];
- const headerIdByName = new Map(headers.map((h) => [h.name, h.id]));
+ const headerId = headerIdFor(headers);
const missingColumns =
rows.length === 0
? []
- : BOM_REQUIRED_COLUMNS.filter((name) => !headerIdByName.has(name));
+ : BOM_REQUIRED_COLUMNS.filter((name) => !headerId(name));
const get = (row: BomRow, name: string) => {
- const id = headerIdByName.get(name);
+ const id = headerId(name);
return id ? cell(row.headerIdToValue?.[id]) : null;
};
diff --git a/packages/ee/src/onshape/panel/properties.test.ts b/packages/ee/src/onshape/panel/properties.test.ts
index 2b488dc40f..e69b38144b 100644
--- a/packages/ee/src/onshape/panel/properties.test.ts
+++ b/packages/ee/src/onshape/panel/properties.test.ts
@@ -428,6 +428,46 @@ describe("resolveMappedFields", () => {
}
]);
});
+
+ it("does not throw on a BOM column Onshape sent without a name", () => {
+ const { unmapped } = resolveMappedFields({
+ properties: [
+ {
+ propertyId: "p-unnamed",
+ name: undefined as unknown as string,
+ valueType: "STRING",
+ value: "X"
+ }
+ ],
+ map: [],
+ definitions: []
+ });
+ expect(unmapped.map((property) => property.propertyId)).toEqual([
+ "p-unnamed"
+ ]);
+ });
+
+ it("treats a relabelled identity column as reserved, whatever its case", () => {
+ const { unmapped } = resolveMappedFields({
+ properties: [
+ {
+ propertyId: "p-pn",
+ name: "PART NUMBER",
+ valueType: "STRING",
+ value: "A-1"
+ },
+ {
+ propertyId: "p-state",
+ name: "STATE",
+ valueType: "STRING",
+ value: "Released"
+ }
+ ],
+ map: [],
+ definitions: []
+ });
+ expect(unmapped).toEqual([]);
+ });
});
describe("resolveMappedFields — item fields", () => {
diff --git a/packages/ee/src/onshape/panel/properties.ts b/packages/ee/src/onshape/panel/properties.ts
index 4469353104..7a742695fc 100644
--- a/packages/ee/src/onshape/panel/properties.ts
+++ b/packages/ee/src/onshape/panel/properties.ts
@@ -135,15 +135,18 @@ export const MAPPABLE_VALUE_TYPES: Record<string, readonly number[]> = {
OBJECT: [CUSTOM_FIELD_DATA_TYPES.text]
};
-/** Identity, description and lifecycle properties: never "not mapped". */
+/**
+ * Identity, description and lifecycle properties: never "not mapped". Lower
+ * case, since a BOM template can relabel a column in any case.
+ */
const RESERVED_PROPERTY_NAMES = new Set([
- "Name",
- "Part number",
- "Revision",
- "Description",
- "State",
- "Exclude from BOM",
- "Not revision managed"
+ "name",
+ "part number",
+ "revision",
+ "description",
+ "state",
+ "exclude from bom",
+ "not revision managed"
]);
function isRecord(value: unknown): value is Record<string, unknown> {
@@ -556,7 +559,10 @@ export function resolveMappedFields({
for (const property of properties) {
if (used.has(property.propertyId)) continue;
const value = propertyDisplayValue(property.value);
- if (value !== null && !RESERVED_PROPERTY_NAMES.has(property.name)) {
+ if (
+ value !== null &&
+ !RESERVED_PROPERTY_NAMES.has(String(property.name).trim().toLowerCase())
+ ) {
unmapped.push({
propertyId: property.propertyId,
name: property.name,