Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add information for extra fields as extensions to openapi specs #10460

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/openapi-v3/src/__tests__/unit/json-to-schema.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import {JsonSchema} from '@loopback/repository-json-schema';
import {expect} from '@loopback/testlab';
import {PropertyDefinition} from '@loopback/repository';

import {jsonOrBooleanToJSON, jsonToSchemaObject, SchemaObject} from '../..';

describe('jsonToSchemaObject', () => {
Expand Down Expand Up @@ -308,6 +310,29 @@ describe('jsonToSchemaObject', () => {
propertyConversionTest(itemsDef, expectedItems);
});

it('add extra properties as extension properties', () => {
const properties: PropertyDefinition = {
type: 'string',
defaultFn: 'guid',
index: false,
precision: 10,
scale: 10,
generated: true,
hidden: false,
};

const expectedItems: SchemaObject = {
type: 'string',
'x-defaultFn': 'guid',
'x-generated': true,
'x-hidden': false,
'x-index': false,
'x-precision': 10,
'x-scale': 10,
};
propertyConversionTest(properties, expectedItems);
});

// Helper function to check conversion of JSON Schema properties
// to Swagger versions
function propertyConversionTest(property: object, expected: object) {
Expand Down
15 changes: 15 additions & 0 deletions packages/openapi-v3/src/json-to-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ export function jsonToSchemaObject(
delete result[converted];
// Check if the description contains information about TypeScript type
const matched = result.description?.match(/^\(tsType: (.+), schemaOptions:/);
const extensionProperties = [
'defaultFn',
'index',
'length',
'precision',
'scale',
'generated',
'hidden',
];
Object.keys(result).forEach(key => {
if (extensionProperties.includes(key)) {
result[`x-${key}`] = result[key];
delete result[key];
}
});
if (matched) {
result['x-typescript-type'] = matched[1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ describe('build-schema', () => {
});
});

it('keeps extensions on property', () => {
expect(
metaToJsonProperty({
type: String,
defaultFn: 'guid',
index: false,
length: 50,
precision: 10,
scale: 0,
generated: true,
hidden: true,
}),
).to.eql({
type: 'string',
defaultFn: 'guid',
index: false,
length: 50,
precision: 10,
scale: 0,
generated: true,
hidden: true,
});
});

it('keeps AJV keywords', () => {
const schema = metaToJsonProperty({
type: String,
Expand Down
20 changes: 18 additions & 2 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@loopback/repository';
import debugFactory from 'debug';
import {inspect} from 'util';
import {JsonSchema} from './index';
import {JsonSchema, ExtensionProperties} from './index';
import {JSON_SCHEMA_KEY} from './keys';
const debug = debugFactory('loopback:repository-json-schema:build-schema');

Expand Down Expand Up @@ -255,9 +255,25 @@ export function isArrayType(type: string | Function | PropertyType) {
* @param meta
*/
export function metaToJsonProperty(meta: PropertyDefinition): JsonSchema {
const propDef: JsonSchema = {};
const propDef: JsonSchema & ExtensionProperties = {};
let result: JsonSchema;
let propertyType = meta.type as string | Function;
const propertiesToCopy = [
'default',
'defaultFn',
'index',
'length',
'precision',
'scale',
'generated',
'hidden',
];

propertiesToCopy.forEach(prop => {
if (meta[prop] !== undefined) {
propDef[prop] = meta[prop];
}
});

if (isArrayType(propertyType) && meta.itemType) {
if (isArrayType(meta.itemType) && !meta.jsonSchema) {
Expand Down
2 changes: 2 additions & 0 deletions packages/repository-json-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export type Optional<T extends object, K extends keyof T = keyof T> = Omit<
K
> &
Partial<Pick<T, K>>;

export type ExtensionProperties = {[x: string]: number | boolean | string};
52 changes: 52 additions & 0 deletions packages/rest/src/__tests__/unit/ajv-factory.provider.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,56 @@ describe('Ajv factory', () => {
ctx = new Context();
ctx.bind(RestBindings.AJV_FACTORY).toProvider(AjvFactoryProvider);
}

it('validate extension properties', async () => {
const ajvFactory = await ctx.get(RestBindings.AJV_FACTORY);
const validator = ajvFactory().compile({
type: 'object',
properties: {
validation: {
type: 'object',
properties: {
'x-default': {
type: 'string',
},
'x-defaultFn': {
type: 'string',
enum: ['guid', 'uuid', 'uuidv4', 'now'],
},
'x-index': {
type: 'boolean',
},
'x-length': {
type: 'number',
},
'x-precision': {
type: 'number',
},
'x-scale': {
type: 'number',
},
'x-generated': {
type: 'boolean',
},
'x-hidden': {
type: 'boolean',
},
},
},
},
});
const result = validator({
validation: {
'x-default': 'default value',
'x-defaultFn': 'guid',
'x-index': false,
'x-length': 54,
'x-precision': 10,
'x-scale': 0,
'x-generated': true,
'x-hidden': false,
},
});
expect(result).to.be.true();
});
});
22 changes: 22 additions & 0 deletions packages/rest/src/validation/ajv-factory.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ export class AjvFactoryProvider implements Provider<AjvFactory> {
const ajvInst = new AjvCtor(ajvOptions);
ajvInst.addKeyword('components');
ajvInst.addKeyword('x-typescript-type');
ajvInst.addKeyword({
keyword: 'x-default',
schemaType: [
'string',
'number',
'integer',
'boolean',
'null',
'object',
'array',
],
});
ajvInst.addKeyword({keyword: 'x-defaultFn', schemaType: ['string']});
ajvInst.addKeyword({
keyword: 'x-index',
schemaType: ['boolean', 'object'],
});
ajvInst.addKeyword({keyword: 'x-length', schemaType: ['number']});
ajvInst.addKeyword({keyword: 'x-precision', schemaType: ['number']});
ajvInst.addKeyword({keyword: 'x-scale', schemaType: ['number']});
ajvInst.addKeyword({keyword: 'x-generated', schemaType: 'boolean'});
ajvInst.addKeyword({keyword: 'x-hidden', schemaType: ['boolean']});

ajvKeywords(ajvInst, validationOptions.ajvKeywords);

Expand Down