Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: false positive errors when validating examples with refs #1377

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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 false positive errors when validating media type examples. Now, the linter won't error on valid examples that contain references.
tatomyr marked this conversation as resolved.
Show resolved Hide resolved
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
Loading