Skip to content

Commit

Permalink
feat(cli): convert OpenAPI schema to JSON schema for model/property
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 25, 2020
1 parent d24e621 commit d0785dd
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 39 deletions.
4 changes: 3 additions & 1 deletion packages/cli/generators/openapi/schema-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
escapePropertyName,
printSpecObject,
toFileName,
asJsonSchema,
} = require('./utils');

function setImport(typeSpec) {
Expand Down Expand Up @@ -162,7 +163,8 @@ function mapObjectType(schema, options) {
// The property name might have chars such as `-`
const propName = escapePropertyName(p);

const propSchemaJson = printSpecObject(propSchema);
const propSchemaJson = asJsonSchema(propSchema);

let propDecoration = `@property({jsonSchema: ${propSchemaJson}})`;

if (required.includes(p)) {
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/generators/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const url = require('url');
const utils = require('../../lib/utils');
const debug = require('../../lib/debug')('openapi-generator');

const toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema');

/**
* Convert a string to title case
* @param {string} str
Expand Down Expand Up @@ -232,6 +234,27 @@ function printSpecObject(specObject) {
);
}

function restoreSpecObject(specObject) {
return _.cloneDeepWith(specObject, value => {
// Restore the original value from `x-$original-value`
if (value != null && value['x-$ref']) {
return json5.parse(value['x-$original-value']);
}
return value;
});
}

/**
* Convert OpenAPI schema to JSON schema
* @param {object} oasSchema - OpenAPI schema
*/
function asJsonSchema(oasSchema) {
const schema = restoreSpecObject(oasSchema);
const jsonSchema = toJsonSchema(schema);
delete jsonSchema['$schema'];
return printSpecObject(jsonSchema);
}

module.exports = {
isExtension,
titleCase,
Expand All @@ -244,5 +267,6 @@ module.exports = {
escapeComment,
printSpecObject,
cloneSpecObject,
asJsonSchema,
validateUrlOrFile,
};
43 changes: 15 additions & 28 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@lerna/project": "^3.18.0",
"@openapi-contrib/openapi-schema-to-json-schema": "^3.0.0",
"@phenomnomnominal/tsquery": "^4.0.0",
"camelcase-keys": "^6.2.2",
"chalk": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down Expand Up @@ -493,6 +495,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down Expand Up @@ -983,6 +987,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down Expand Up @@ -1294,6 +1300,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down Expand Up @@ -1984,6 +1992,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ export class Error {
@property({required: true, jsonSchema: {
type: 'integer',
format: 'int32',
minimum: -2147483648,
maximum: 2147483647,
}})
code: number;
Expand Down
Loading

0 comments on commit d0785dd

Please sign in to comment.