genoc 0.5.2 → 0.5.3

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
@@ -58,10 +58,11 @@ import type { Requester } from 'genoc/runtime';
58
58
  For the full `Requester` type signature and binary and stream handling, see [Usage](./docs/usage.md).
59
59
 
60
60
  ```typescript
61
- import { createClient } from './client.js';
62
61
  import type { Requester } from 'genoc/runtime';
63
62
  import { RequesterFailError, ErrorResponse } from 'genoc/runtime';
64
63
 
64
+ import { createClient } from './client.js';
65
+
65
66
  const baseUrl = 'https://api.example.com';
66
67
 
67
68
  const requester: Requester = async (method, path, options) => {
@@ -3,7 +3,7 @@ import { buildSchemaRenameMap, buildTypeJsDoc, sanitizeJsDocText, sanitizeTypeNa
3
3
  import { operationEmissions, RESERVED_TYPE_NAMES } from '../utils/operation-naming.js';
4
4
  import { escapeStringLiteral } from '../utils/string.js';
5
5
  import { buildDiscriminatorRegistry, renameAwareTypeName } from './discriminator-registry.js';
6
- import { analyzePathsDetailed } from './path-analyzer.js';
6
+ import { analyzePathsDetailed, collectFormProperties, findBinaryFieldNames, schemaContainsBinary, } from './path-analyzer.js';
7
7
  import { SchemaMapper } from './schema-mapper.js';
8
8
  /**
9
9
  * Indent unit matching `INDENT_UNIT` in schema-mapper.ts (2 spaces, pinned by
@@ -338,6 +338,7 @@ function analyzeServers(servers) {
338
338
  export function analyze(doc, opts = {}) {
339
339
  const resolver = opts.resolver ?? new RefResolver(doc);
340
340
  const strategy = opts.strategy ?? 'path-based';
341
+ const warn = opts.warnSink ?? writeWarning;
341
342
  const schemaNameList = doc.components?.schemas ? Object.keys(doc.components.schemas) : [];
342
343
  const renameMap = buildSchemaRenameMap(schemaNameList, RESERVED_TYPE_NAMES);
343
344
  for (const [original, renamed] of renameMap) {
@@ -360,7 +361,7 @@ export function analyze(doc, opts = {}) {
360
361
  // source for discriminator translation: the mapper's ref-translation seam
361
362
  // consumes it, and the always-emitted `{Base}Variant` unions below are
362
363
  // emitted from it.
363
- const discriminatorRegistry = buildDiscriminatorRegistry(doc.components?.schemas, resolver, renameMap, allSchemaNames, writeWarning);
364
+ const discriminatorRegistry = buildDiscriminatorRegistry(doc.components?.schemas, resolver, renameMap, allSchemaNames, warn);
364
365
  const mapper = new SchemaMapper(resolver, {
365
366
  typeNameGenerator: renamingTypeGenerator,
366
367
  emittedNames: allSchemaNames,
@@ -472,14 +473,28 @@ export function analyze(doc, opts = {}) {
472
473
  if (emissions.headers) {
473
474
  opLines.push(`export type ${emissions.headers} = ${buildParamTypeBody(op.headerParams, mapper)};`);
474
475
  }
476
+ // A urlencoded body declaring `format: binary` fields cannot transport
477
+ // them — the generated client would silently type the files as plain
478
+ // strings. Warn and name the offending fields (merged through allOf).
479
+ if (op.requestBody?.contentTypes[0] === 'application/x-www-form-urlencoded' &&
480
+ rawBodySchema !== undefined &&
481
+ schemaContainsBinary(rawBodySchema, resolver)) {
482
+ const fieldNames = findBinaryFieldNames(rawBodySchema, resolver);
483
+ const fieldList = fieldNames.length > 0
484
+ ? ` declaring binary fields (${fieldNames.map((n) => `"${n}"`).join(', ')})`
485
+ : ' declaring binary data';
486
+ warn(`Warning: the request body of ${op.method.toUpperCase()} ${op.path} uses content type "application/x-www-form-urlencoded"${fieldList}, but urlencoded form data cannot transport binary files. File uploads require "multipart/form-data"; the generated client types these fields as plain strings.\n`);
487
+ }
475
488
  // Section 3: Request body types
476
489
  const bodyJsDoc = op.requestBody ? buildDescriptionJsDoc(op.requestBody.description) : '';
477
490
  if (op.requestBody?.isMultipart && rawBodySchema !== undefined) {
478
- const schema = resolver.resolveSchema(rawBodySchema);
479
- const requiredSet = new Set(schema.required ?? []);
491
+ // Form fields come from the allOf-merged walk: a body declaring its
492
+ // fields via `allOf` members (resolving `$ref`s) previously produced an
493
+ // empty property walk — `Record<string, never>` and dropped appends.
494
+ const { properties, required: requiredSet } = collectFormProperties(rawBodySchema, resolver);
480
495
  const propLines = [];
481
496
  const fileUploadProperties = [];
482
- for (const [name, propSchema] of Object.entries(schema.properties ?? {})) {
497
+ for (const [name, propSchema] of properties) {
483
498
  const resolved = resolver.resolveSchema(propSchema);
484
499
  const optional = requiredSet.has(name) ? '' : '?';
485
500
  let tsType;
@@ -82,6 +82,37 @@ export interface AnalyzedOperation {
82
82
  /** Raw operation-level `security`; `undefined` = inherit the document default. */
83
83
  security?: SecurityRequirementObject[];
84
84
  }
85
+ /**
86
+ * Deep, ref-resolving, cycle-safe check: does any schema node reachable from
87
+ * `schema` (through `properties`, `items`, `allOf`/`oneOf`/`anyOf` members and
88
+ * schema-valued `additionalProperties`) declare `format: binary`? Unlike
89
+ * {@link isBinarySchema} this catches binary data buried in composed schemas —
90
+ * e.g. a `files` array of binary strings hidden inside an `allOf` member.
91
+ */
92
+ export declare function schemaContainsBinary(schema: SchemaObject | ReferenceObject | undefined, resolver: RefResolver): boolean;
93
+ /**
94
+ * The flattened form fields of a request body schema: the root object's own
95
+ * `properties`/`required` merged with those of every `allOf` member,
96
+ * recursively, resolving `$ref`s. Later same-named members do not override
97
+ * earlier ones (first-write wins, matching the mapper's `allOf` array-order
98
+ * rendering); member `required` lists are unioned.
99
+ *
100
+ * Motivation: multipart bodies declaring their fields via `allOf` (a common
101
+ * upstream-spec shape) previously produced an empty property walk — the
102
+ * generated body type collapsed to `Record<string, never>` and every FormData
103
+ * append was silently dropped.
104
+ */
105
+ export declare function collectFormProperties(schema: SchemaObject | ReferenceObject | undefined, resolver: RefResolver, properties?: Map<string, SchemaObject | ReferenceObject>, required?: Set<string>, visited?: Set<SchemaObject>): {
106
+ properties: Map<string, SchemaObject | ReferenceObject>;
107
+ required: Set<string>;
108
+ };
109
+ /**
110
+ * Names of the (allOf-merged) top-level form fields whose subtree contains
111
+ * `format: binary` — for warnings that name the offending fields. Empty when
112
+ * the binary data sits outside any named field (e.g. the whole body schema
113
+ * is a binary string) or there is no binary data at all.
114
+ */
115
+ export declare function findBinaryFieldNames(schema: SchemaObject | ReferenceObject | undefined, resolver: RefResolver): string[];
85
116
  /**
86
117
  * Analyzer-internal analysis result: the analyzed operations plus the raw
87
118
  * request body schemas in a PARALLEL ARRAY (same length and order as
@@ -12,6 +12,103 @@ function isBinarySchema(schema, resolver) {
12
12
  return false;
13
13
  return resolver.resolveSchema(schema).format === 'binary';
14
14
  }
15
+ /**
16
+ * Deep, ref-resolving, cycle-safe check: does any schema node reachable from
17
+ * `schema` (through `properties`, `items`, `allOf`/`oneOf`/`anyOf` members and
18
+ * schema-valued `additionalProperties`) declare `format: binary`? Unlike
19
+ * {@link isBinarySchema} this catches binary data buried in composed schemas —
20
+ * e.g. a `files` array of binary strings hidden inside an `allOf` member.
21
+ */
22
+ export function schemaContainsBinary(schema, resolver) {
23
+ return containsBinary(schema, resolver, new Set());
24
+ }
25
+ function containsBinary(schema, resolver, visited) {
26
+ if (!schema)
27
+ return false;
28
+ const resolved = resolver.resolveSchema(schema);
29
+ // Cycle guard on resolved object identity (a self-referencing $ref chain
30
+ // would otherwise recurse forever; schema-mapper uses the same pattern).
31
+ if (visited.has(resolved))
32
+ return false;
33
+ visited.add(resolved);
34
+ if (resolved.format === 'binary')
35
+ return true;
36
+ if (resolved.items !== undefined) {
37
+ if (containsBinary(resolved.items, resolver, visited))
38
+ return true;
39
+ }
40
+ for (const member of resolved.allOf ?? []) {
41
+ if (containsBinary(member, resolver, visited))
42
+ return true;
43
+ }
44
+ for (const member of resolved.oneOf ?? []) {
45
+ if (containsBinary(member, resolver, visited))
46
+ return true;
47
+ }
48
+ for (const member of resolved.anyOf ?? []) {
49
+ if (containsBinary(member, resolver, visited))
50
+ return true;
51
+ }
52
+ for (const propSchema of Object.values(resolved.properties ?? {})) {
53
+ if (containsBinary(propSchema, resolver, visited))
54
+ return true;
55
+ }
56
+ if (typeof resolved.additionalProperties === 'object' && resolved.additionalProperties !== null) {
57
+ if (containsBinary(resolved.additionalProperties, resolver, visited))
58
+ return true;
59
+ }
60
+ return false;
61
+ }
62
+ /**
63
+ * The flattened form fields of a request body schema: the root object's own
64
+ * `properties`/`required` merged with those of every `allOf` member,
65
+ * recursively, resolving `$ref`s. Later same-named members do not override
66
+ * earlier ones (first-write wins, matching the mapper's `allOf` array-order
67
+ * rendering); member `required` lists are unioned.
68
+ *
69
+ * Motivation: multipart bodies declaring their fields via `allOf` (a common
70
+ * upstream-spec shape) previously produced an empty property walk — the
71
+ * generated body type collapsed to `Record<string, never>` and every FormData
72
+ * append was silently dropped.
73
+ */
74
+ export function collectFormProperties(schema, resolver, properties = new Map(), required = new Set(), visited = new Set()) {
75
+ if (!schema)
76
+ return { properties, required };
77
+ const resolved = resolver.resolveSchema(schema);
78
+ // Cycle guard on resolved identity (same pattern as containsBinary above).
79
+ if (visited.has(resolved))
80
+ return { properties, required };
81
+ visited.add(resolved);
82
+ // allOf members come first, mirroring the mapper's `A & B` member order.
83
+ for (const member of resolved.allOf ?? []) {
84
+ collectFormProperties(member, resolver, properties, required, visited);
85
+ }
86
+ for (const name of resolved.required ?? []) {
87
+ required.add(name);
88
+ }
89
+ for (const [name, propSchema] of Object.entries(resolved.properties ?? {})) {
90
+ if (!properties.has(name)) {
91
+ properties.set(name, propSchema);
92
+ }
93
+ }
94
+ return { properties, required };
95
+ }
96
+ /**
97
+ * Names of the (allOf-merged) top-level form fields whose subtree contains
98
+ * `format: binary` — for warnings that name the offending fields. Empty when
99
+ * the binary data sits outside any named field (e.g. the whole body schema
100
+ * is a binary string) or there is no binary data at all.
101
+ */
102
+ export function findBinaryFieldNames(schema, resolver) {
103
+ const { properties } = collectFormProperties(schema, resolver);
104
+ const names = [];
105
+ for (const [name, propSchema] of properties) {
106
+ if (containsBinary(propSchema, resolver, new Set())) {
107
+ names.push(name);
108
+ }
109
+ }
110
+ return names;
111
+ }
15
112
  function resolveParameter(param, resolver) {
16
113
  return resolver.resolve(param);
17
114
  }
@@ -267,6 +267,14 @@ export class SchemaMapper {
267
267
  return result;
268
268
  }
269
269
  if (s.type === undefined) {
270
+ // OpenAPI permits omitting `type`: a schema carrying `properties`
271
+ // and/or `additionalProperties` is an object schema regardless. Map it
272
+ // through the object branch instead of degrading to `unknown`; a
273
+ // genuinely unconstrained schema (no keywords to infer from) stays
274
+ // `unknown` (any JSON value).
275
+ if (s.properties !== undefined || s.additionalProperties !== undefined) {
276
+ return this.mapObject(s, name, context, visited, indent, spine);
277
+ }
270
278
  return { tsType: 'unknown', imports: [] };
271
279
  }
272
280
  if (Array.isArray(s.type)) {
@@ -148,4 +148,6 @@ export interface AnalyzeOptions {
148
148
  * Omitted is treated as `'3.1'`.
149
149
  */
150
150
  effectiveVersion?: '3.0' | '3.1';
151
+ /** Warning sink; defaults to `process.stderr.write`. Mirrors the mapper's `warnSink`. */
152
+ warnSink?: (message: string) => void;
151
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genoc",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Generate typed HTTP clients from OpenAPI 3.0 / 3.1 specifications",
5
5
  "keywords": [
6
6
  "client",
@@ -48,12 +48,12 @@
48
48
  },
49
49
  "devDependencies": {
50
50
  "@changesets/cli": "^2.31.0",
51
- "@types/node": "^26.6.2",
52
- "oxfmt": "^0.70.0",
53
- "oxlint": "^1.85.0",
51
+ "@types/node": "^26.6.5",
52
+ "oxfmt": "^0.75.0",
53
+ "oxlint": "^1.90.0",
54
54
  "typescript": "^6.0.3",
55
- "undici": "^8",
56
- "vitest": "^5.0.1"
55
+ "undici": "^8.12.3",
56
+ "vitest": "^5.0.5"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "undici": "^6.13.0 || ^7 || ^8"