Skip to content

Commit

Permalink
denormalize consumes and produces (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Mar 5, 2024
1 parent 93134c4 commit 82fe38d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "openapi-workspaces",
"license": "MIT",
"private": true,
"version": "0.54.8",
"version": "0.54.9",
"workspaces": [
"projects/json-pointer-helpers",
"projects/openapi-io",
Expand Down
2 changes: 1 addition & 1 deletion projects/fastify-capture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/fastify-capture",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/json-pointer-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/json-pointer-helpers",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-io",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ exports[`denormalize v2 denormalizes shared path parameters 1`] = `
},
},
],
"produces": [
"application/json",
"application/json+something",
],
"responses": {
"200": {
"description": "some thing",
Expand Down Expand Up @@ -87,6 +91,9 @@ exports[`denormalize v2 denormalizes shared path parameters 1`] = `
},
},
],
"produces": [
"application/json+something",
],
"responses": {
"200": {
"description": "some thing",
Expand All @@ -101,6 +108,7 @@ exports[`denormalize v2 denormalizes shared path parameters 1`] = `
"files": [
{
"contents": "swagger: '2.0.1'
produces: ['application/json+something']
info:
title: 'some thing'
version: 'v0'
Expand All @@ -125,6 +133,7 @@ paths:
example: d5b640e5-d88c-4c17-9bf0-93597b7a1ce2
- $ref: parameters.yml#/something
get:
produces: ['application/json']
responses:
200:
description: 'some thing'
Expand All @@ -144,7 +153,7 @@ paths:
",
"index": null,
"path": "/src/denormalizers/__tests__/specs/v2/openapi.yaml",
"sha256": "42778528330e8611b2fe132c99dc7c5f8d92663b6a06d8f7b1d0ec226cc929b6",
"sha256": "8d13ebe93e4152fe53308e1d7365040aeb4828fb99b79c022e4afb950a9477d5",
},
{
"contents": "something:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
swagger: '2.0.1'
produces: ['application/json+something']
info:
title: 'some thing'
version: 'v0'
Expand All @@ -23,6 +24,7 @@ paths:
example: d5b640e5-d88c-4c17-9bf0-93597b7a1ce2
- $ref: parameters.yml#/something
get:
produces: ['application/json']
responses:
200:
description: 'some thing'
Expand Down
26 changes: 24 additions & 2 deletions projects/openapi-io/src/denormalizers/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function denormalize<
} as T;

if (version === '2.x.x') {
const consumes: string[] | undefined = (parse.jsonLike as any).consumes;
const produces: string[] | undefined = (parse.jsonLike as any).produces;
delete (parse.jsonLike as any).consumes;
delete (parse.jsonLike as any).produces;
for (const [pathKey, path] of Object.entries(parse.jsonLike.paths ?? {})) {
if (path) {
denormalizePathsV2(path, pathKey, parse.sourcemap, warnings);
Expand All @@ -44,7 +48,7 @@ export function denormalize<
if (operation) {
denormalizeOperationV2(
operation,
{ path: pathKey, method },
{ path: pathKey, method, consumes, produces },
parse.sourcemap,
warnings
);
Expand Down Expand Up @@ -141,14 +145,32 @@ export function denormalizeOperationV2(
{
path,
method,
produces,
consumes,
}: {
path: string;
method: string;
produces?: string[];
consumes?: string[];
},
sourcemap?: JsonSchemaSourcemap,
warnings?: string[]
) {
// Nothing to denormalize for v2 yet
if (consumes) {
if (operation.consumes) {
operation.consumes = [...new Set([...operation.consumes, ...consumes])];
} else {
operation.consumes = [...consumes];
}
}

if (produces) {
if (operation.produces) {
operation.produces = [...new Set([...operation.produces, ...produces])];
} else {
operation.produces = [...produces];
}
}
}

export function denormalizePathsV3(
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-utilities",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/optic",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/rulesets-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/rulesets-base",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/standard-rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/standard-rulesets",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.54.8",
"version": "0.54.9",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down

0 comments on commit 82fe38d

Please sign in to comment.