Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-planets-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/openapi-core": patch
---

Fixed incorrect validation logic for the `constructor` property.
8 changes: 8 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ describe('E2E', () => {
const result = getCommandOutput(args, {}, { testPath });
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot_2.txt'));
});

test('lint file with constructor property in schema', async () => {
const testPath = join(__dirname, 'fixtures/constructor-property');
const args = getParams(indexEntryPoint, ['lint', 'openapi.yaml']);

const result = getCommandOutput(args, {}, { testPath });
await expect(cleanupOutput(result)).toMatchFileSnapshot(join(testPath, 'snapshot.txt'));
});
});

describe('zero-config', () => {
Expand Down
37 changes: 37 additions & 0 deletions __tests__/fixtures/constructor-property/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.0.0
servers:
- url: http://test.com:3000
info:
title: Test API - Constructor Property
version: 1.0.0
license:
name: MIT
url: https://opensource.org/licenses/MIT
components:
schemas:
SchemaWithConstructor:
type: object
properties:
id:
type: string
constructor: # This is the property we are testing
type: object
description: 'A property named constructor'
properties:
foo:
type: string
paths:
/test:
get:
summary: Test endpoint
security: []
operationId: test
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaWithConstructor'
'400':
description: Bad Request
8 changes: 8 additions & 0 deletions __tests__/fixtures/constructor-property/snapshot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

No configurations were provided -- using built in recommended configuration by default.

validating openapi.yaml...
openapi.yaml: validated in <test>ms

Woohoo! Your API description is valid. 🎉

4 changes: 2 additions & 2 deletions packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isExternalValue,
} from './ref-utils.js';
import { isNamedType, SpecExtension } from './types/index.js';
import { readFileFromUrl, parseYaml, nextTick } from './utils.js';
import { readFileFromUrl, parseYaml, nextTick, getOwn } from './utils.js';

import type { YAMLNode, LoadOptions } from 'yaml-ast-parser';
import type { NormalizedNodeType } from './types/index.js';
Expand Down Expand Up @@ -299,7 +299,7 @@ export async function resolveDocument(opts: {

for (const propName of Object.keys(node)) {
let propValue = node[propName];
let propType = type.properties[propName];
let propType = getOwn(type.properties, propName);
if (propType === undefined) propType = type.additionalProperties;
if (typeof propType === 'function') propType = propType(propValue, propName);
if (propType === undefined) propType = unknownType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isRef } from '../../ref-utils.js';
import { getOwn } from '../../utils.js';

import type { Oas2Rule, Oas3Rule } from '../../visitors.js';
import type { Oas3Schema, Oas3_1Schema } from '../../typings/openapi.js';
Expand Down Expand Up @@ -41,7 +42,7 @@ export const NoRequiredSchemaPropertiesUndefined: Oas3Rule | Oas2Rule = () => {
const allProperties = elevateProperties(schema);

for (const [i, requiredProperty] of schema.required.entries()) {
if (!allProperties || allProperties[requiredProperty] === undefined) {
if (!allProperties || getOwn(allProperties, requiredProperty) === undefined) {
report({
message: `Required property '${requiredProperty}' is undefined.`,
location: location.child(['required', i]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SpecVersion } from '../../oas-types.js';
import { getOwn } from '../../utils.js';

import type { Oas2Rule, Oas3Rule } from '../../visitors.js';
import type { UserContext } from '../../walk.js';
Expand All @@ -14,7 +15,7 @@ export const ScalarPropertyMissingExample: Oas3Rule | Oas2Rule = () => {
{ report, location, oasVersion, resolve }: UserContext
) {
for (const propName of Object.keys(properties)) {
const propSchema = resolve(properties[propName]).node;
const propSchema = resolve(getOwn(properties, propName)).node;

if (!propSchema || !isScalarSchema(propSchema)) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rules/common/struct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNamedType, SpecExtension } from '../../types/index.js';
import { oasTypeOf, matchesJsonSchemaType, getSuggest, validateSchemaEnumType } from '../utils.js';
import { isRef } from '../../ref-utils.js';
import { isPlainObject } from '../../utils.js';
import { getOwn, isPlainObject } from '../../utils.js';

import type { UserContext } from '../../walk.js';
import type {
Expand Down Expand Up @@ -102,7 +102,7 @@ export const Struct:
const propLocation = location.child([propName]);
let propValue = node[propName];

let propType = type.properties[propName];
let propType = getOwn(type.properties, propName);
if (propType === undefined) propType = type.additionalProperties;
if (typeof propType === 'function') propType = propType(propValue, propName);

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ export function dequal(foo: any, bar: any): boolean {
return foo !== foo && bar !== bar;
}

export function getOwn(obj: Record<string, any>, key: string) {
return obj.hasOwnProperty(key) ? obj[key] : undefined;
}

export type CollectFn = (value: unknown) => void;

export type StrictObject<T extends object> = T & { [key: string]: undefined };
4 changes: 2 additions & 2 deletions packages/core/src/walk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Location, isRef } from './ref-utils.js';
import { pushStack, popStack } from './utils.js';
import { pushStack, popStack, getOwn } from './utils.js';
import { YamlParseError, makeRefId } from './resolve.js';
import { isNamedType, SpecExtension } from './types/index.js';

Expand Down Expand Up @@ -308,7 +308,7 @@ export function walkDocument<T extends BaseVisitor>(opts: {
loc = location; // properties on the same level as $ref should resolve against original location, not target
}

let propType = type.properties[propName];
let propType = getOwn(type.properties, propName);
if (propType === undefined) propType = type.additionalProperties;
if (typeof propType === 'function') propType = propType(value, propName);

Expand Down
Loading