-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(OpenApi3.1-Yaml): add support for Server and ServerVariable
Refs #1
- Loading branch information
Showing
18 changed files
with
276 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 27 additions & 3 deletions
30
apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/predicates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
import { isYamlKeyValuePair } from 'apidom-ast'; | ||
import { pathSatisfies, startsWith, both, curry } from 'ramda'; | ||
import { isYamlKeyValuePair, isYamlMapping, isYamlScalar } from 'apidom-ast'; | ||
import { pathSatisfies, startsWith, both, curry, filter, anyPass } from 'ramda'; | ||
|
||
// hasKey :: String -> YamlKeyValuePair -> Boolean | ||
const hasKey = curry((keyName, node) => { | ||
const { key } = node; | ||
|
||
if (!isYamlScalar(key)) { | ||
return false; | ||
} | ||
|
||
return key.content === keyName; | ||
}); | ||
|
||
// hasKeys :: [String] -> [YamlKeyValuePair] -> Boolean | ||
const hasKeys = curry((keyNames, keyValuePairs) => { | ||
const predicates = keyNames.map((keyName: string) => hasKey(keyName)); | ||
return filter(anyPass(predicates), keyValuePairs).length === keyNames.length; | ||
}); | ||
|
||
// isOpenApiExtension :: Options -> YamlKeyValuePair -> Boolean | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const isOpenApiExtension = curry((options, node) => | ||
both(isYamlKeyValuePair, pathSatisfies(startsWith('x-'), ['key', 'content']))(node), | ||
); | ||
|
||
// isServerObject :: Options -> YamlMapping -> Boolean | ||
export const isServerObject = curry((options, node) => { | ||
if (!isYamlMapping(node)) { | ||
return false; | ||
} | ||
return hasKeys(['url'], node.content); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ser-adapter-openapi3-1-yaml/src/parser/visitors/generics/FixedFieldsYamlMappingVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...idom-parser-adapter-openapi3-1-yaml/src/parser/visitors/generics/MapYamlMappingVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import stampit from 'stampit'; | ||
import { isNonEmptyString } from 'ramda-adjunct'; | ||
|
||
import PatternedFieldsYamlMappingVisitor from './PatternedFieldsYamlMappingVisitor'; | ||
|
||
const MapYamlMappingVisitor = stampit(PatternedFieldsYamlMappingVisitor, { | ||
props: { | ||
fieldPatternPredicate: isNonEmptyString, | ||
}, | ||
}); | ||
|
||
export default MapYamlMappingVisitor; |
76 changes: 76 additions & 0 deletions
76
...adapter-openapi3-1-yaml/src/parser/visitors/generics/PatternedFieldsYamlMappingVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import stampit from 'stampit'; | ||
import { F as stubFalse } from 'ramda'; | ||
import { noop } from 'ramda-adjunct'; | ||
import { YamlKeyValuePair, YamlMapping } from 'apidom-ast'; | ||
|
||
import SpecificationVisitor from '../SpecificationVisitor'; | ||
import { isOpenApiExtension } from '../../predicates'; | ||
import { visit } from '..'; | ||
|
||
const PatternedFieldsYamlMappingVisitor = stampit(SpecificationVisitor, { | ||
props: { | ||
fieldPatternPredicate: stubFalse, | ||
specPath: noop, | ||
ignoredFields: [], | ||
keyMap: { | ||
// @ts-ignore | ||
[YamlMapping.type]: ['children'], | ||
}, | ||
canSupportSpecificationExtensions: false, | ||
}, | ||
init({ | ||
// @ts-ignore | ||
specPath = this.specPath, | ||
// @ts-ignore | ||
ignoredFields = this.ignoredFields, | ||
// @ts-ignore | ||
canSupportSpecificationExtensions = this.canSupportSpecificationExtensions, | ||
} = {}) { | ||
this.specPath = specPath; | ||
this.ignoredFields = ignoredFields; | ||
this.canSupportSpecificationExtensions = canSupportSpecificationExtensions; | ||
}, | ||
methods: { | ||
mapping(mappingNode: YamlMapping) { | ||
this.maybeAddSourceMap(mappingNode, this.element); | ||
}, | ||
|
||
keyValuePair(keyValuePairNode: YamlKeyValuePair) { | ||
const { key: keyNode, value: valueNode } = keyValuePairNode; | ||
const keyName = keyNode.content; | ||
const { MemberElement } = this.namespace.elements.Element.prototype; | ||
|
||
if (this.canSupportSpecificationExtensions && isOpenApiExtension({}, keyValuePairNode)) { | ||
const visitor = this.retrieveVisitorInstance(['document', 'extension']); | ||
visit(keyValuePairNode, visitor); | ||
this.element.content.push(visitor.element); | ||
} else if (!this.ignoredFields.includes(keyName) && this.fieldPatternPredicate(keyName)) { | ||
const specPath = this.specPath(valueNode); | ||
const visitor = this.retrieveVisitorInstance(specPath); | ||
const keyElement = new this.namespace.elements.String(keyName); | ||
|
||
visit(keyValuePairNode, visitor); | ||
|
||
const memberElement = this.maybeAddSourceMap( | ||
keyValuePairNode, | ||
new MemberElement(this.maybeAddSourceMap(keyNode, keyElement), visitor.element), | ||
); | ||
memberElement.classes.push('patternedField'); | ||
|
||
this.element.content.push(memberElement); | ||
} else if (!this.ignoredFields.includes(keyName)) { | ||
const keyElement = new this.namespace.elements.String(keyName); | ||
const memberElement = this.maybeAddSourceMap( | ||
keyValuePairNode, | ||
new MemberElement( | ||
this.maybeAddSourceMap(keyNode, keyElement), | ||
this.nodeToElement(['kind'], valueNode), | ||
), | ||
); | ||
this.element.content.push(memberElement); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default PatternedFieldsYamlMappingVisitor; |
33 changes: 33 additions & 0 deletions
33
.../apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/ServersVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import stampit from 'stampit'; | ||
import { YamlSequence } from 'apidom-ast'; | ||
|
||
import { BREAK } from '..'; | ||
import SpecificationVisitor from '../SpecificationVisitor'; | ||
import { isServerObject } from '../../predicates'; | ||
import { KindVisitor } from '../generics'; | ||
|
||
const ServersVisitor = stampit(KindVisitor, SpecificationVisitor, { | ||
init() { | ||
this.element = new this.namespace.elements.Array(); | ||
this.element.classes.push('servers'); | ||
}, | ||
methods: { | ||
sequence(sequenceNode: YamlSequence) { | ||
sequenceNode.content.forEach((item) => { | ||
if (isServerObject({}, item)) { | ||
const element = this.nodeToElement(['document', 'objects', 'Server'], item); | ||
this.element.push(element); | ||
} else { | ||
const element = this.nodeToElement(['kind'], item); | ||
this.element.push(element); | ||
} | ||
}); | ||
|
||
this.maybeAddSourceMap(sequenceNode, this.element); | ||
|
||
return BREAK; | ||
}, | ||
}, | ||
}); | ||
|
||
export default ServersVisitor; |
7 changes: 7 additions & 0 deletions
7
...dapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server-variable/DefaultVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const DefaultVisitor = stampit(KindVisitor); | ||
|
||
export default DefaultVisitor; |
7 changes: 7 additions & 0 deletions
7
...er-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server-variable/DescriptionVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const DescriptionVisitor = stampit(KindVisitor); | ||
|
||
export default DescriptionVisitor; |
7 changes: 7 additions & 0 deletions
7
...r-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server-variable/EnumVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const EnumVisitor = stampit(KindVisitor); | ||
|
||
export default EnumVisitor; |
17 changes: 17 additions & 0 deletions
17
...-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server-variable/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import FixedFieldsYamlMappingVisitor from '../../generics/FixedFieldsYamlMappingVisitor'; | ||
import { KindVisitor } from '../../generics'; | ||
|
||
const ServerVariableVisitor = stampit(KindVisitor, FixedFieldsYamlMappingVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'ServerVariable']), | ||
canSupportSpecificationExtensions: true, | ||
}, | ||
init() { | ||
this.element = new this.namespace.elements.ServerVariable(); | ||
}, | ||
}); | ||
|
||
export default ServerVariableVisitor; |
7 changes: 7 additions & 0 deletions
7
...ser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server/DescriptionVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const DescriptionVisitor = stampit(KindVisitor); | ||
|
||
export default DescriptionVisitor; |
7 changes: 7 additions & 0 deletions
7
...idom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server/UrlVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
|
||
const UrlVisitor = stampit(KindVisitor); | ||
|
||
export default UrlVisitor; |
17 changes: 17 additions & 0 deletions
17
...arser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server/VariablesVisitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import { KindVisitor } from '../../generics'; | ||
import MapYamlMappingVisitor from '../../generics/MapYamlMappingVisitor'; | ||
|
||
const VariablesVisitor = stampit(KindVisitor, MapYamlMappingVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'ServerVariable']), | ||
}, | ||
init() { | ||
this.element = new this.namespace.elements.Object(); | ||
this.element.classes.push('variables'); | ||
}, | ||
}); | ||
|
||
export default VariablesVisitor; |
16 changes: 16 additions & 0 deletions
16
...es/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/server/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import FixedFieldsYamlMappingVisitor from '../../generics/FixedFieldsYamlMappingVisitor'; | ||
import { KindVisitor } from '../../generics'; | ||
|
||
const ServerVisitor = stampit(KindVisitor, FixedFieldsYamlMappingVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'Server']), | ||
}, | ||
init() { | ||
this.element = new this.namespace.elements.Server(); | ||
}, | ||
}); | ||
|
||
export default ServerVisitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,17 @@ info: | |
x-username: char0n | ||
url: https://www.linkedin.com/in/vladimirgorej/ | ||
email: [email protected] | ||
servers: | ||
- url: http://api.example.com/v1 | ||
description: Optional server description, e.g. Main (production) server | ||
- url: http:{port}//staging-api.example.com | ||
description: Optional server description, e.g. Internal staging server for testing | ||
variables: | ||
port: | ||
enum: | ||
- '8443' | ||
- '443' | ||
default: '8443' | ||
description: Port description | ||
... | ||
prop: value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters