@kubb/adapter-oas 5.0.0-beta.27 → 5.0.0-beta.29

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
@@ -782,12 +782,6 @@ function getPrimitiveType(type) {
782
782
  return "string";
783
783
  }
784
784
  /**
785
- * Narrows a content-type string to the `MediaType` union Kubb recognizes, or returns `null`.
786
- */
787
- function getMediaType(contentType) {
788
- return Object.values(_kubb_core.ast.mediaTypes).includes(contentType) ? contentType : null;
789
- }
790
- /**
791
785
  * Returns all parameters for an operation, merging path-level and operation-level entries.
792
786
  * Operation-level parameters override path-level ones with the same `in:name` key.
793
787
  * `$ref` parameters resolve via `dereferenceWithRef` for backward compatibility.
@@ -1119,6 +1113,31 @@ function getRequestBodyContentTypes(document, operation) {
1119
1113
  if (!body) return [];
1120
1114
  return body.content ? Object.keys(body.content) : [];
1121
1115
  }
1116
+ /**
1117
+ * Returns all response content type keys for an operation at a given status code.
1118
+ *
1119
+ * Response `$ref`s are resolved in-place first — the same mutation `getResponseSchema` performs —
1120
+ * so the returned list reflects the available content types even for referenced responses.
1121
+ *
1122
+ * @example
1123
+ * ```ts
1124
+ * getResponseBodyContentTypes(document, operation, 200)
1125
+ * // ['application/json', 'application/xml']
1126
+ * ```
1127
+ */
1128
+ function getResponseBodyContentTypes(document, operation, statusCode) {
1129
+ if (operation.schema.responses) {
1130
+ const responses = operation.schema.responses;
1131
+ for (const key in responses) {
1132
+ const schema = responses[key];
1133
+ if (schema && isReference(schema)) responses[key] = resolveRef(document, schema.$ref);
1134
+ }
1135
+ }
1136
+ const responseObj = operation.getResponseByStatusCode(statusCode);
1137
+ if (!responseObj || typeof responseObj !== "object" || isReference(responseObj)) return [];
1138
+ const body = responseObj;
1139
+ return body.content ? Object.keys(body.content) : [];
1140
+ }
1122
1141
  //#endregion
1123
1142
  //#region src/parser.ts
1124
1143
  /**
@@ -1823,20 +1842,30 @@ function createSchemaParser(ctx) {
1823
1842
  } : void 0;
1824
1843
  const responses = operation.getResponseStatusCodes().map((statusCode) => {
1825
1844
  const responseObj = operation.getResponseByStatusCode(statusCode);
1826
- const responseSchema = getResponseSchema(document, operation, statusCode, { contentType: ctx.contentType });
1827
1845
  const responseName = operationName ? `${operationName}Status${statusCode}` : void 0;
1828
- const schema = responseSchema && Object.keys(responseSchema).length > 0 ? parseSchema({
1829
- schema: responseSchema,
1830
- name: responseName
1831
- }, options) : _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.emptySchemaType) });
1832
- const { description, content } = getResponseMeta(responseObj);
1833
- const mediaType = content ? getMediaType(Object.keys(content)[0] ?? "") : getMediaType(operation.contentType ?? "");
1846
+ const { description } = getResponseMeta(responseObj);
1847
+ const parseEntrySchema = (contentType) => {
1848
+ const raw = getResponseSchema(document, operation, statusCode, { contentType });
1849
+ return {
1850
+ schema: raw && Object.keys(raw).length > 0 ? parseSchema({
1851
+ schema: raw,
1852
+ name: responseName
1853
+ }, options) : _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.emptySchemaType) }),
1854
+ keysToOmit: collectPropertyKeysByFlag(raw, "writeOnly")
1855
+ };
1856
+ };
1857
+ const content = (ctx.contentType ? [ctx.contentType] : getResponseBodyContentTypes(document, operation, statusCode)).map((contentType) => ({
1858
+ contentType,
1859
+ ...parseEntrySchema(contentType)
1860
+ }));
1861
+ if (content.length === 0) content.push({
1862
+ contentType: operation.contentType || "application/json",
1863
+ ...parseEntrySchema(ctx.contentType)
1864
+ });
1834
1865
  return _kubb_core.ast.createResponse({
1835
1866
  statusCode,
1836
1867
  description,
1837
- schema,
1838
- mediaType,
1839
- keysToOmit: collectPropertyKeysByFlag(responseSchema, "writeOnly")
1868
+ content
1840
1869
  });
1841
1870
  });
1842
1871
  const urlPath = new URLPath(operation.path);