-
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(reference): add Workflows 1.0.0 JSON parser plugin (#3568)
Refs #3567
- Loading branch information
1 parent
8c2e246
commit 1b12b50
Showing
6 changed files
with
358 additions
and
0 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
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
42 changes: 42 additions & 0 deletions
42
packages/apidom-reference/src/parse/parsers/workflows-json-1/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,42 @@ | ||
import stampit from 'stampit'; | ||
import { pick } from 'ramda'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { parse, mediaTypes, detect } from '@swagger-api/apidom-parser-adapter-workflows-json-1'; | ||
|
||
import ParserError from '../../../errors/ParserError'; | ||
import { File as IFile, Parser as IParser } from '../../../types'; | ||
import Parser from '../Parser'; | ||
|
||
const WorkflowsJson1Parser: stampit.Stamp<IParser> = stampit(Parser, { | ||
props: { | ||
name: 'workflows-json-1', | ||
fileExtensions: ['.json'], | ||
mediaTypes, | ||
}, | ||
methods: { | ||
async canParse(file: IFile): Promise<boolean> { | ||
const hasSupportedFileExtension = | ||
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension); | ||
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType); | ||
|
||
if (!hasSupportedFileExtension) return false; | ||
if (hasSupportedMediaType) return true; | ||
if (!hasSupportedMediaType) { | ||
return detect(file.toString()); | ||
} | ||
return false; | ||
}, | ||
async parse(file: IFile): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: any) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default WorkflowsJson1Parser; |
92 changes: 92 additions & 0 deletions
92
packages/apidom-reference/test/parse/parsers/workflows-json-1/fixtures/sample-workflow.json
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,92 @@ | ||
{ | ||
"workflowsSpec": "1.0.0", | ||
"info": { | ||
"title": "A pet purchasing workflow", | ||
"summary": "This workflow showcases how to purchase a pet through a sequence of API calls", | ||
"description": "This workflow walks you through the steps of `searching` for, `selecting`, and `purchasing` an available pet.\n", | ||
"version": "1.0.1" | ||
}, | ||
"sourceDescriptions": [ | ||
{ | ||
"name": "petStoreDescription", | ||
"url": "https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml", | ||
"type": "openapi" | ||
} | ||
], | ||
"workflows": [ | ||
{ | ||
"workflowId": "loginUserRetrievePet", | ||
"summary": "Login User and then retrieve pets", | ||
"description": "This procedure lays out the steps to login a user and then retrieve pets", | ||
"inputs": { | ||
"type": "object", | ||
"properties": { | ||
"username": { | ||
"type": "string" | ||
}, | ||
"password": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"steps": [ | ||
{ | ||
"stepId": "loginStep", | ||
"description": "This step demonstrates the user login step", | ||
"operationId": "petStoreDescription.loginUser", | ||
"parameters": [ | ||
{ | ||
"name": "username", | ||
"in": "query", | ||
"value": "$inputs.username" | ||
}, | ||
{ | ||
"name": "password", | ||
"in": "query", | ||
"value": "$inputs.password" | ||
} | ||
], | ||
"successCriteria": [ | ||
{ | ||
"condition": "$statusCode == 200" | ||
} | ||
], | ||
"outputs": { | ||
"tokenExpires": "$response.header.X-Expires-After", | ||
"rateLimit": "$response.header.X-Rate-Limit", | ||
"sessionToken": "$response.body" | ||
} | ||
}, | ||
{ | ||
"stepId": "getPetStep", | ||
"description": "retrieve a pet by status from the GET pets endpoint", | ||
"operationRef": "https://petstore3.swagger.io/api/v3/openapi.json#/paths/users/~findbystatus~1{status}/get", | ||
"dependsOn": "loginStep", | ||
"parameters": [ | ||
{ | ||
"name": "status", | ||
"in": "query", | ||
"value": "available" | ||
}, | ||
{ | ||
"name": "Authorization", | ||
"in": "header", | ||
"value": "$steps.loginUser.outputs.sessionToken" | ||
} | ||
], | ||
"successCriteria": [ | ||
{ | ||
"condition": "$statusCode == 200" | ||
} | ||
], | ||
"outputs": { | ||
"availablePets": "$response.body" | ||
} | ||
} | ||
], | ||
"outputs": { | ||
"available": "$steps.getPetStep.availablePets" | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.