@sidecar-ai/compiler 0.1.0-alpha.15 → 0.1.0-alpha.16

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/dist/index.js CHANGED
@@ -519,9 +519,18 @@ function typeToJsonSchemaInner(type) {
519
519
  if (type.isUnion()) {
520
520
  const parts = type.getUnionTypes().filter((part) => !part.isUndefined());
521
521
  if (parts.every(isLiteralType)) {
522
- return { enum: parts.map((part) => literalValue(part)) };
522
+ return { enum: [...new Set(parts.map((part) => literalValue(part)))] };
523
523
  }
524
- return { anyOf: parts.map((part) => typeToJsonSchema(part)) };
524
+ const schemas = deduplicateSchemas(
525
+ parts.map((part) => typeToJsonSchema(part))
526
+ );
527
+ if (schemas.length === 1) {
528
+ return schemas[0];
529
+ }
530
+ return {
531
+ ...schemas.every((schema) => schema.type === "object") ? { type: "object" } : {},
532
+ anyOf: schemas
533
+ };
525
534
  }
526
535
  const properties = type.getProperties();
527
536
  const indexType = type.getStringIndexType() ?? type.getNumberIndexType();
@@ -536,6 +545,37 @@ function typeToJsonSchemaInner(type) {
536
545
  }
537
546
  return {};
538
547
  }
548
+ function deduplicateSchemas(schemas) {
549
+ const seen = /* @__PURE__ */ new Set();
550
+ return schemas.filter((schema) => {
551
+ const key = schemaComparisonKey(schema);
552
+ if (seen.has(key)) {
553
+ return false;
554
+ }
555
+ seen.add(key);
556
+ return true;
557
+ });
558
+ }
559
+ function schemaComparisonKey(schema) {
560
+ return JSON.stringify(canonicalSchemaValue(schema));
561
+ }
562
+ function canonicalSchemaValue(value, keyword) {
563
+ if (Array.isArray(value)) {
564
+ const values = value.map((item) => canonicalSchemaValue(item));
565
+ if (keyword === "allOf" || keyword === "anyOf" || keyword === "enum" || keyword === "oneOf" || keyword === "required" || keyword === "type") {
566
+ return values.sort(
567
+ (left, right) => JSON.stringify(left).localeCompare(JSON.stringify(right))
568
+ );
569
+ }
570
+ return values;
571
+ }
572
+ if (value && typeof value === "object") {
573
+ return Object.fromEntries(
574
+ Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entry]) => [key, canonicalSchemaValue(entry, key)])
575
+ );
576
+ }
577
+ return value;
578
+ }
539
579
  function objectTypeToSchema(properties, indexType) {
540
580
  const schemaProperties = {};
541
581
  const required = [];