Skip to content

Commit d65a17f

Browse files
Hage Yaapahacksparrow
authored andcommitted
feat: support built-in JavaScript/Node schema types
Add support for built-in JavaScript/Node schema types in Model properties
1 parent b7c60c5 commit d65a17f

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

packages/openapi-v3/src/generate-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function resolveSchema(
2828
} else if (fn === Boolean) {
2929
resolvedSchema = {type: 'boolean'};
3030
} else if (fn === Date) {
31-
resolvedSchema = {type: 'string', format: 'date'};
31+
resolvedSchema = {type: 'string', format: 'date-time'};
3232
} else if (fn === Object) {
3333
resolvedSchema = {type: 'object'};
3434
} else if (fn === Array) {

packages/openapi-v3/test/unit/generate-schema.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('generate-schema unit tests', () => {
2424
});
2525

2626
it('resolves type Date', () => {
27-
expect(resolveSchema(Date)).to.eql({type: 'string', format: 'date'});
27+
expect(resolveSchema(Date)).to.eql({type: 'string', format: 'date-time'});
2828
});
2929

3030
it('resolves type Object', () => {

packages/repository-json-schema/src/build-schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function isComplexType(ctor: Function) {
9090
Object,
9191
Function,
9292
Array,
93+
Date,
9394
] as Function[]).includes(ctor);
9495
}
9596

@@ -125,6 +126,11 @@ export function metaToJsonProperty(meta: PropertyDefinition): JSONSchema {
125126

126127
if (isComplexType(propertyType)) {
127128
Object.assign(propDef, {$ref: `#/definitions/${propertyType.name}`});
129+
} else if (propertyType === Date) {
130+
Object.assign(propDef, {
131+
type: 'string',
132+
format: 'date-time',
133+
});
128134
} else {
129135
Object.assign(propDef, {
130136
type: <JSONSchemaTypeName>propertyType.name.toLowerCase(),

packages/repository-json-schema/test/integration/build-schema.integration.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ describe('build-schema', () => {
137137
expectValidJsonSchema(jsonSchema);
138138
});
139139

140+
it('properly converts date property', () => {
141+
@model()
142+
class TestModel {
143+
@property()
144+
date: Date;
145+
}
146+
147+
const jsonSchema = modelToJsonSchema(TestModel);
148+
expect(jsonSchema.properties).to.deepEqual({
149+
date: {
150+
type: 'string',
151+
format: 'date-time',
152+
},
153+
});
154+
expectValidJsonSchema(jsonSchema);
155+
});
156+
140157
it('properly converts object properties', () => {
141158
@model()
142159
class TestModel {

packages/repository-json-schema/test/unit/build-schema.unit.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ describe('build-schema', () => {
7070
expect(isComplexType(Function)).to.eql(false);
7171
});
7272

73+
it('returns false for Date', () => {
74+
expect(isComplexType(Date)).to.eql(false);
75+
});
76+
7377
it('returns false for Array', () => {
7478
expect(isComplexType(Array)).to.eql(false);
7579
});
@@ -88,24 +92,74 @@ describe('build-schema', () => {
8892
);
8993
});
9094

91-
it('converts types in strings', () => {
92-
expect(metaToJsonProperty({type: 'number'})).to.eql({
93-
type: 'number',
95+
it('converts Boolean', () => {
96+
expect(metaToJsonProperty({type: Boolean})).to.eql({
97+
type: 'boolean',
9498
});
9599
});
96100

97-
it('converts primitives', () => {
101+
it('converts String', () => {
102+
expect(metaToJsonProperty({type: String})).to.eql({
103+
type: 'string',
104+
});
105+
});
106+
107+
it('converts Number', () => {
98108
expect(metaToJsonProperty({type: Number})).to.eql({
99109
type: 'number',
100110
});
101111
});
102112

103-
it('converts arrays', () => {
113+
it('converts Date', () => {
114+
expect(metaToJsonProperty({type: Date})).to.eql({
115+
type: 'string',
116+
format: 'date-time',
117+
});
118+
});
119+
120+
it('converts Object', () => {
121+
expect(metaToJsonProperty({type: Object})).to.eql({
122+
type: 'object',
123+
});
124+
});
125+
126+
it('converts Array', () => {
104127
expect(metaToJsonProperty({type: Array})).to.eql({
105128
type: 'array',
106129
});
107130
});
108131

132+
it('converts "boolean" in strings', () => {
133+
expect(metaToJsonProperty({type: 'boolean'})).to.eql({
134+
type: 'boolean',
135+
});
136+
});
137+
138+
it('converts "string" in strings', () => {
139+
expect(metaToJsonProperty({type: 'string'})).to.eql({
140+
type: 'string',
141+
});
142+
});
143+
144+
it('converts "date" in strings', () => {
145+
expect(metaToJsonProperty({type: 'date'})).to.eql({
146+
type: 'string',
147+
format: 'date-time',
148+
});
149+
});
150+
151+
it('converts "object" in strings', () => {
152+
expect(metaToJsonProperty({type: 'object'})).to.eql({
153+
type: 'object',
154+
});
155+
});
156+
157+
it('converts "array" in strings', () => {
158+
expect(metaToJsonProperty({type: 'array'})).to.eql({
159+
type: 'array',
160+
});
161+
});
162+
109163
it('converts complex types', () => {
110164
class CustomType {}
111165
expect(metaToJsonProperty({type: CustomType})).to.eql({

0 commit comments

Comments
 (0)