@kubb/plugin-faker 5.0.4 → 5.0.6

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.cjs CHANGED
@@ -467,10 +467,14 @@ function buildResponseUnionSchema(node, resolver) {
467
467
  }
468
468
  return kubb_kit.ast.factory.createSchema({
469
469
  type: "union",
470
- members: responses.map((response) => kubb_kit.ast.factory.createSchema({
471
- type: "ref",
472
- name: resolver.response.status(node, response.statusCode)
473
- }))
470
+ members: responses.map((response) => {
471
+ const schema = (response.content?.length ?? 0) === 1 ? response.content?.[0]?.schema : void 0;
472
+ return kubb_kit.ast.factory.createSchema({
473
+ type: "ref",
474
+ name: resolver.response.status(node, response.statusCode),
475
+ schema
476
+ });
477
+ })
474
478
  });
475
479
  }
476
480
  const SCALAR_TYPES$1 = /* @__PURE__ */ new Set([
@@ -969,27 +973,23 @@ const fakerKeywordMapper = {
969
973
  email: () => "faker.internet.email()",
970
974
  blob: () => "faker.image.url() as unknown as Blob"
971
975
  };
972
- function getEnumValues(node) {
973
- if (node.namedEnumValues?.length) return node.namedEnumValues.map((item) => item.value);
974
- return node.enumValues ?? [];
975
- }
976
976
  function parseEnumValue(value) {
977
977
  if (typeof value === "string") return stringify(value);
978
978
  return value;
979
979
  }
980
980
  /**
981
981
  * Reads the discriminator literal off a variant, or `undefined` when it can't be determined.
982
+ *
983
+ * A variant carries exactly one discriminator value, so a property resolving to several literals
984
+ * (a `ref` to the enum shared by every branch, say) identifies no branch and narrows nothing.
982
985
  */
983
986
  function getDiscriminatorValue(member, discriminatorPropertyName) {
984
- const prop = kubb_kit.ast.narrowSchema(member, "object")?.properties?.find((property) => property.name === discriminatorPropertyName);
985
- const enumNode = prop ? kubb_kit.ast.narrowSchema(prop.schema, "enum") : null;
986
- if (enumNode) return getEnumValues(enumNode)[0];
987
- const refNode = kubb_kit.ast.narrowSchema(member, "ref");
988
- if (refNode?.schema) return getDiscriminatorValue(refNode.schema, discriminatorPropertyName);
989
- const intersectionNode = kubb_kit.ast.narrowSchema(member, "intersection");
990
- for (const intersectionMember of intersectionNode?.members ?? []) {
991
- const value = getDiscriminatorValue(intersectionMember, discriminatorPropertyName);
992
- if (value !== void 0) return value;
987
+ for (const property of kubb_kit.ast.resolveSchemaProperties({
988
+ node: member,
989
+ propertyName: discriminatorPropertyName
990
+ })) {
991
+ const values = kubb_kit.ast.getSchemaLiteralValues(property.schema);
992
+ if (values.length === 1) return values[0];
993
993
  }
994
994
  }
995
995
  /**
@@ -1055,7 +1055,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
1055
1055
  return `${resolvedName}${this.options.nestedInUnion && (node.schema?.type === "object" || node.schema?.type === "intersection") ? "<object>" : ""}()`;
1056
1056
  },
1057
1057
  enum(node) {
1058
- return fakerKeywordMapper.enum(getEnumValues(node).map(parseEnumValue), this.options.typeName);
1058
+ return fakerKeywordMapper.enum(kubb_kit.ast.getSchemaLiteralValues(node).map(parseEnumValue), this.options.typeName);
1059
1059
  },
1060
1060
  union(node) {
1061
1061
  const { discriminatorPropertyName } = node;
@@ -1295,7 +1295,8 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
1295
1295
  type: "union",
1296
1296
  members: variants.map((variant) => kubb_kit.ast.factory.createSchema({
1297
1297
  type: "ref",
1298
- name: variant.name
1298
+ name: variant.name,
1299
+ schema: variant.schema
1299
1300
  }))
1300
1301
  });
1301
1302
  return [...variants.map((variant) => ({