Skip to content

Commit 55ad835

Browse files
authored
Added dockerized node.js based tool to convert jsonschema files to openapi files (#252)
added tool to convert jsonschema to openapi schema in scripts folder updated director accordingly director test does not skip jsonschema anymore
1 parent 9d244de commit 55ad835

38 files changed

+526
-580
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Docs:
2+
# This tool is based on [Phil Sturgeon blog](https://philsturgeon.uk/api/2018/04/13/openapi-and-json-schema-divergence-solved/)
3+
# and specifically on [json-schema-to-openapi-schema](https://github.com/wework/json-schema-to-openapi-schema)
4+
#
5+
# The tool converts any .json file present in a specific input folder into openapi schema .yaml in a specific output folder
6+
7+
# Usage:
8+
# docker build -t json2openapiconverter .
9+
# docker run -v %INPUT_FOLDER%:/input -v %OUTPUT_FOLDER%:/output json2openapiconverter
10+
11+
12+
13+
FROM node
14+
15+
LABEL maintainer=sanderegg
16+
17+
VOLUME [ "/input" ]
18+
VOLUME [ "/output" ]
19+
20+
WORKDIR /src
21+
22+
RUN npm install --save json-schema-to-openapi-schema && \
23+
npm install --save js-yaml
24+
COPY converter.js /src/converter.js
25+
26+
27+
CMD [ "node", "/src/converter.js" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const toOpenApi = require('json-schema-to-openapi-schema');
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const yaml = require('js-yaml')
6+
7+
const inputPath = '/input';
8+
const outputPath = '/output/';
9+
10+
11+
const filenames = fs.readdirSync(inputPath)
12+
13+
filenames.forEach(filepath => {
14+
const extName = path.extname(filepath);
15+
if (extName == ".json") {
16+
console.log("converting " + filepath + "...");
17+
const contents = fs.readFileSync(inputPath + "/" + filepath, 'utf8');
18+
const object = JSON.parse(contents);
19+
const convertedSchema = toOpenApi(object);
20+
console.log("converted " + filepath + " succesfully");
21+
22+
var yamlSchema = yaml.safeDump(convertedSchema)
23+
// console.log(yamlSchema);
24+
25+
// there is a BUG here. examples in a schema are not allowed in openapi, they should be replaced by example
26+
// Note that schemas and properties support singular example but not plural examples.
27+
// [link to problem](https://swagger.io/docs/specification/adding-examples/)
28+
yamlSchema = yamlSchema.replace(/examples:\n/g, "example:\n");
29+
// write as yaml
30+
fs.writeFileSync(outputPath + path.basename(filepath, ".json") + ".yaml", yamlSchema);
31+
// fs.writeFileSync(outputPath + "converted_" + filepath, JSON.stringify(convertedSchema));
32+
}
33+
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AssignmentUuid:
2+
in: query
3+
name: service_uuid
4+
description: The uuid to assign the service with
5+
required: true
6+
schema:
7+
type: string
8+
format: uuid
9+
example: 123e4567-e89b-12d3-a456-426655440000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ServiceKey:
2+
in: query
3+
name: service_key
4+
description: The key (url) of the service
5+
required: true
6+
schema:
7+
type: string
8+
format: url
9+
example: simcore/services/dynamic/3d-viewer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ServiceKeyPath:
2+
in: path
3+
name: service_key
4+
description: The key (url) of the service
5+
required: true
6+
schema:
7+
type: string
8+
format: url
9+
example: simcore/services/dynamic/3d-viewer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ServiceType:
2+
in: query
3+
name: service_type
4+
description: |
5+
The service type:
6+
* computational - a computational service
7+
* interactive - an interactive service
8+
required: false
9+
schema:
10+
type: string
11+
enum:
12+
- computational
13+
- interactive
14+
example: computational
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ServiceUuid:
2+
in: path
3+
name: service_uuid
4+
description: The uuid of the service
5+
required: true
6+
schema:
7+
type: string
8+
format: uuid
9+
example: 123e4567-e89b-12d3-a456-426655440000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ServiceVersion:
2+
in: query
3+
name: service_tag
4+
description: The tag/version of the service
5+
required: false
6+
schema:
7+
type: string
8+
example: "1.4"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ServiceVersionPath:
2+
in: path
3+
name: service_version
4+
description: The tag/version of the service
5+
required: true
6+
schema:
7+
type: string
8+
example: "1.4"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ErrorEnveloped:
2+
type: object
3+
properties:
4+
data:
5+
$ref: '#/Error'
6+
status:
7+
type: integer
8+
example: 404
9+
Error:
10+
type: object
11+
required:
12+
- status
13+
- message
14+
properties:
15+
message:
16+
description: Error message
17+
type: string
18+
example: Unexpected error
19+
errors:
20+
type: array
21+
items:
22+
properties:
23+
code:
24+
type: string
25+
description: Server Exception
26+
example: ServiceUUIDNotFoundError
27+
status:
28+
description: Error code
29+
type: integer
30+
example: 404
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
HealthCheckEnveloped:
2+
type: object
3+
properties:
4+
data:
5+
$ref: '#/HealthCheck'
6+
status:
7+
type: integer
8+
example: 200
9+
HealthCheck:
10+
type: object
11+
properties:
12+
name:
13+
type: string
14+
example: director service
15+
status:
16+
type: string
17+
example: SERVICE_RUNNING
18+
api_version:
19+
type: string
20+
example: 1.0.0-dev
21+
version:
22+
type: string
23+
example: 1dfcfdc

0 commit comments

Comments
 (0)