Skip to content

Commit

Permalink
fix: false positive errors when validating examples with refs (Redocl…
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr authored Jan 19, 2024
1 parent 94db1ea commit 5573a61
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-rules-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed a problem where the linter incorrectly returned an error for valid examples that contain references.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
components:
schemas:
A:
type: object
properties:
a:
type: string
b:
$ref: '#/components/schemas/B'
B:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.1.0
components:
schemas:
C:
$ref: './components.yaml#/components/schemas/A'
D:
$ref: './components.yaml#/components/schemas/A'
paths:
/pet:
get:
responses:
200:
content:
application/json:
example: { 'a': 'test', 'b': 'test' }
schema:
$ref: '#/components/schemas/C'
application/x+json:
example: { 'a': 'test', 'b': 'test' }
schema:
$ref: '#/components/schemas/D'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apis:
main:
root: ./openapi.yaml

rules:
no-invalid-media-type-examples: error
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E lint no-invalid-media-type-examples-multiple-valid-refs-different-files 1`] = `
validating /openapi.yaml...
/openapi.yaml: validated in <test>ms
Woohoo! Your API description is valid. 🎉
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
openapi: 3.1.0
paths:
/foo:
get:
responses:
'200':
content:
application/json:
schema:
type: object
properties:
id:
$ref: '#/components/schemas/foo'
examples:
ValidFoo:
value:
id: 1
/bar:
get:
responses:
'200':
content:
application/json:
schema:
type: object
properties:
id:
$ref: '#/components/schemas/bar'
examples:
ValidBar:
value:
id: 1
application/baz+json:
schema:
type: object
properties:
id:
$ref: '#/components/schemas/baz'
examples:
ValidBaz:
value: #
id: 3

components:
schemas:
commonId:
type: integer
foo:
$ref: '#/components/schemas/commonId'
bar:
$ref: '#/components/schemas/commonId'
baz:
$ref: '#/components/schemas/commonId'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apis:
main:
root: ./openapi.yaml

rules:
no-invalid-media-type-examples: error
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E lint no-invalid-media-type-examples-multiple-valid-refs 1`] = `
validating /openapi.yaml...
/openapi.yaml: validated in <test>ms
Woohoo! Your API description is valid. 🎉
`;
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions packages/core/src/rules/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ajv, { ValidateFunction, ErrorObject } from '@redocly/ajv/dist/2020';
import Ajv from '@redocly/ajv/dist/2020';
import { Location, escapePointer } from '../ref-utils';
import { ResolveFn } from '../walk';

import type { ValidateFunction, ErrorObject } from '@redocly/ajv/dist/2020';
import type { ResolveFn } from '../walk';

let ajvInstance: Ajv | null = null;

Expand All @@ -21,10 +23,10 @@ function getAjv(resolve: ResolveFn, allowAdditionalProperties: boolean) {
allowUnionTypes: true,
validateFormats: false, // TODO: fix it
defaultUnevaluatedProperties: allowAdditionalProperties,
loadSchemaSync(base: string, $ref: string) {
loadSchemaSync(base: string, $ref: string, $id: string) {
const resolvedRef = resolve({ $ref }, base.split('#')[0]);
if (!resolvedRef || !resolvedRef.location) return false;
return { $id: resolvedRef.location.absolutePointer, ...resolvedRef.node };
return { $id: resolvedRef.location.source.absoluteRef + '#' + $id, ...resolvedRef.node };
},
logger: false,
});
Expand Down

0 comments on commit 5573a61

Please sign in to comment.