Skip to content

Commit

Permalink
Have better allof warning (#2766)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Mar 4, 2024
1 parent cf0f6b1 commit 14d84c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ paths:
}
`;

exports[`denormalize allOf merging does not merge allOf when all items are not all objects: warnings 1`] = `
[
"invalid allOf variant at src/denormalizers/__tests__/specs/allOf/no-merge.yaml:26:578 (/paths/~1example/get/requestBody/content/application~1json/schema/properties/d/allOf/1)",
]
`;

exports[`denormalize allOf merging merges allOf when all items are objects 1`] = `
{
"jsonLike": {
Expand Down Expand Up @@ -242,6 +248,8 @@ paths:
}
`;

exports[`denormalize allOf merging merges allOf when all items are objects: warnings 1`] = `[]`;

exports[`denormalize allOf merging merges allOf with only one item 1`] = `
{
"jsonLike": {
Expand Down Expand Up @@ -305,6 +313,8 @@ paths:
}
`;

exports[`denormalize allOf merging merges allOf with only one item: warnings 1`] = `[]`;

exports[`denormalize allOf merging merges allOfs in type array object / items 1`] = `
{
"jsonLike": {
Expand Down Expand Up @@ -536,6 +546,8 @@ paths:
}
`;

exports[`denormalize allOf merging merges allOfs in type array object / items: warnings 1`] = `[]`;

exports[`denormalize allOf merging merges nested allOf 1`] = `
{
"jsonLike": {
Expand Down Expand Up @@ -691,6 +703,8 @@ paths:
}
`;

exports[`denormalize allOf merging merges nested allOf: warnings 1`] = `[]`;

exports[`denormalize denormalizes shared path parameters 1`] = `
{
"jsonLike": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ describe('denormalize', () => {
const spec = await parseOpenAPIWithSourcemap(
path.resolve(openapiFilePath)
);

const denormalized = denormalize(spec);
const warnings: string[] = [];
const denormalized = denormalize(spec, warnings);

expect(prepSnapshot(denormalized)).toMatchSnapshot();
expect(warnings).toMatchSnapshot('warnings');
});
});
});
30 changes: 26 additions & 4 deletions projects/openapi-io/src/denormalizers/denormalizeProperty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { jsonPointerHelpers } from '@useoptic/json-pointer-helpers';
import { FlatOpenAPIV3, OAS3 } from '@useoptic/openapi-utilities';
import {
FlatOpenAPIV3,
OAS3,
getSourcemapLink,
sourcemapReader,
} from '@useoptic/openapi-utilities';
import { JsonSchemaSourcemap } from '../parser/sourcemap';
import { logPointer } from './pointer';

Expand Down Expand Up @@ -119,10 +124,27 @@ export function denormalizeProperty(
sourcemap,
pointers
);
let maybeReader:
| ReturnType<typeof sourcemapReader>['findFileAndLines']
| undefined = undefined;
if (sourcemap) {
maybeReader = sourcemapReader(sourcemap).findFileAndLines;
}
warnings.push(
...invalidChildren.map(
([, i]) => `invalid allOf variant at ${pointers.old}/allOf/${i}`
),
...invalidChildren.map(([, i]) => {
const jsonPath = jsonPointerHelpers.append(
pointers.old,
'allOf',
String(i)
);
const maybeSourcemap = maybeReader
? maybeReader(jsonPath)
: maybeReader;
const location = maybeSourcemap
? `${getSourcemapLink(maybeSourcemap)} (${jsonPath})`
: jsonPath;
return `invalid allOf variant at ${location}`;
}),
...w
);
effectiveObject = obj;
Expand Down

0 comments on commit 14d84c0

Please sign in to comment.