Skip to content

Commit

Permalink
feat(reference): add OAS 3.1 Example.externalValue dereference
Browse files Browse the repository at this point in the history
Refs #467
  • Loading branch information
char0n committed Jul 1, 2021
1 parent 7c52686 commit 751188f
Show file tree
Hide file tree
Showing 27 changed files with 367 additions and 6 deletions.
7 changes: 4 additions & 3 deletions apidom/packages/apidom-ns-openapi-3-1/src/elements/Example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Attributes, Meta, ObjectElement, StringElement } from 'minim';
import { Attributes, Meta, Element } from 'minim';
import { ObjectElement, StringElement } from 'apidom';

class Example extends ObjectElement {
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
Expand All @@ -22,11 +23,11 @@ class Example extends ObjectElement {
this.set('description', description);
}

get value(): StringElement {
get value(): Element {
return this.get('value');
}

set value(value: StringElement) {
set value(value: Element) {
this.set('value', value);
}

Expand Down
1 change: 1 addition & 0 deletions apidom/packages/apidom-ns-openapi-3-1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
isCallbackElement,
isComponentsElement,
isContactElement,
isExampleElement,
isExternalDocumentationElement,
isInfoElement,
isJsonSchemaDialectElement,
Expand Down
13 changes: 13 additions & 0 deletions apidom/packages/apidom-ns-openapi-3-1/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createPredicate, isStringElement } from 'apidom';
import CallbackElement from './elements/Callback';
import ComponentsElement from './elements/Components';
import ContactElement from './elements/Contact';
import ExampleElement from './elements/Example';
import ExternalDocumentationElement from './elements/ExternalDocumentation';
import HeaderElement from './elements/Header';
import InfoElement from './elements/Info';
Expand Down Expand Up @@ -63,6 +64,18 @@ export const isContactElement = createPredicate(
},
);

export const isExampleElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
const isElementTypeExample = isElementType('example');
const primitiveEqObject = primitiveEq('object');

return either(
is(ExampleElement),
allPass([hasBasicElementProps, isElementTypeExample, primitiveEqObject]),
);
},
);

export const isExternalDocumentationElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
const isElementTypeExternalDocumentation = isElementType('externalDocumentation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PathItemElement,
LinkElement,
OperationElement,
ExampleElement,
SchemaElement,
isReferenceElementExternal,
isPathItemElementExternal,
Expand Down Expand Up @@ -253,9 +254,9 @@ const OpenApi3_1DereferenceVisitor = stampit({
return undefined;
}

// operationRef and operationId are mutually exclusive
// operationRef and operationId fields are mutually exclusive
if (isStringElement(linkElement.operationRef) && isStringElement(linkElement.operationId)) {
throw new Error('LinkElement operationRef and operationId are mutually exclusive.');
throw new Error('LinkElement operationRef and operationId fields are mutually exclusive.');
}

// @ts-ignore
Expand Down Expand Up @@ -288,6 +289,30 @@ const OpenApi3_1DereferenceVisitor = stampit({
return undefined;
},

async ExampleElement(exampleElement: ExampleElement) {
// ignore ExampleElement without externalValue field
if (!isStringElement(exampleElement.externalValue)) {
return undefined;
}

// ignore resolving ExampleElement externalValue
if (!this.options.resolve.external && isStringElement(exampleElement.externalValue)) {
return undefined;
}

// value and externalValue fields are mutually exclusive
if (exampleElement.hasKey('value') && isStringElement(exampleElement.externalValue)) {
throw new Error('ExampleElement value and externalValue fields are mutually exclusive.');
}

const reference = await this.toReference(exampleElement.externalValue.toValue());

// eslint-disable-next-line no-param-reassign
exampleElement.value = reference.value.result;

return undefined;
},

async SchemaElement(referencingElement: SchemaElement) {
/**
* Skip traversal for already visited schemas and all their child schemas.
Expand Down

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./favicon.ico"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.json"
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"value": {
"prop1": "value1",
"prop2": "value2"
},
"externalValue": "./ex.json"
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prop1": "value1",
"prop2": "value2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"value": {
"prop1": "value1",
"prop2": "value2"
},
"externalValue": "./ex.json#/json/pointer"
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prop1": "value1",
"prop2": "value2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.json#/json/pointer"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"value": "dmFsMTt2YWwyO3ZhbDMK",
"externalValue": "./ex.csv"
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val1;val2;val3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.csv"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"value": "sample value",
"externalValue": "./ex.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"value": {
"prop1": "value1",
"prop2": "value2"
},
"externalValue": "./ex.yaml"
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
prop1: value1
prop2: value2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"components": {
"examples": {
"example1": {
"description": "example1 description",
"externalValue": "./ex.yaml"
}
}
}
}
Loading

0 comments on commit 751188f

Please sign in to comment.