Skip to content

Commit

Permalink
fix: support title and description
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed May 2, 2018
1 parent 720c8c8 commit b89ac31
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ entry:

+ `@entry request-protocol.json`: the entry file name

common:

+ `@title foo`: set `title = 'foo'`
+ `@description bar`: set `description = 'bar'`

number:

+ `@multipleOf 10`: set `multipleOf = 10`
Expand Down
1 change: 1 addition & 0 deletions demo/cases.gql
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type NumberType {
sfixed64Member: Int!
floatMember: Float!
doubleMember: Float!
titleMember: Float!
}

type StringType {
Expand Down
8 changes: 7 additions & 1 deletion demo/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
},
"doubleMember": {
"type": "number"
},
"titleMember": {
"type": "number",
"title": "foo",
"description": "bar"
}
},
"required": [
Expand All @@ -195,7 +200,8 @@
"fixed64Member",
"sfixed64Member",
"floatMember",
"doubleMember"
"doubleMember",
"titleMember"
],
"additionalProperties": false
},
Expand Down
1 change: 1 addition & 0 deletions demo/cases.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type numberType = {
sfixed64Member: int;
floatMember: float;
doubleMember: float;
titleMember: float;
}

type stringType = {
Expand Down
1 change: 1 addition & 0 deletions demo/cases.proto
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ message NumberType {
sfixed64 sfixed64Member = 12;
float floatMember = 13;
double doubleMember = 14;
double titleMember = 15;
}

message StringType {
Expand Down
1 change: 1 addition & 0 deletions demo/cases.re
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type numberType = {
sfixed64Member: int,
floatMember: float,
doubleMember: float,
titleMember: float,
};

type stringType = {
Expand Down
6 changes: 6 additions & 0 deletions demo/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ type NumberType = {

floatMember: float;
doubleMember: double;

/**
* @title foo
* @description bar
*/
titleMember: number;
}

type StringType = {
Expand Down
13 changes: 11 additions & 2 deletions demo/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,19 @@
"kind": "number",
"type": "double"
}
},
{
"name": "titleMember",
"type": {
"kind": "number",
"type": "number",
"title": "foo",
"description": "bar"
}
}
],
"minProperties": 14,
"maxProperties": 14
"minProperties": 15,
"maxProperties": 15
},
{
"kind": "object",
Expand Down
6 changes: 6 additions & 0 deletions online/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ type NumberType = {
floatMember: float;
doubleMember: double;
/**
* @title foo
* @description bar
*/
titleMember: number;
}
type StringType = {
Expand Down
30 changes: 25 additions & 5 deletions src/json-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function getJsonSchemaProperty(memberType: Type | ObjectModel | ArrayModel | Uni
} else if (memberType.kind === 'boolean') {
return {
type: 'boolean',
default: memberType.default
default: memberType.default,
title: memberType.title,
description: memberType.description
}
} else if (memberType.kind === 'map') {
return {
Expand All @@ -55,7 +57,9 @@ function getJsonSchemaProperty(memberType: Type | ObjectModel | ArrayModel | Uni
uniqueItems: memberType.uniqueItems,
minItems: memberType.minItems,
maxItems: memberType.maxItems,
default: memberType.default
default: memberType.default,
title: memberType.title,
description: memberType.description
}
} else if (memberType.kind === 'enum') {
if (memberType.enums.length === 1) {
Expand Down Expand Up @@ -112,7 +116,9 @@ function getJsonSchemaPropertyOfString(memberType: StringType) {
minLength: memberType.minLength,
maxLength: memberType.maxLength,
pattern: memberType.pattern,
default: memberType.default
default: memberType.default,
title: memberType.title,
description: memberType.description
}
}

Expand Down Expand Up @@ -140,7 +146,9 @@ function getJsonSchemaPropertyOfObject(memberType: ObjectModel | ObjectType): De
additionalProperties,
minProperties: memberType.minProperties > memberType.members.filter(m => !m.optional).length ? memberType.minProperties : undefined,
maxProperties: memberType.maxProperties && memberType.maxProperties < memberType.members.length ? memberType.maxProperties : undefined,
default: memberType.default
default: memberType.default,
title: memberType.title,
description: memberType.description
}
}

Expand Down Expand Up @@ -217,7 +225,9 @@ function getNumberType(numberType: NumberType): Definition {
multipleOf: numberType.multipleOf,
exclusiveMinimum: numberType.exclusiveMinimum,
exclusiveMaximum: numberType.exclusiveMaximum,
default: numberType.default
default: numberType.default,
title: numberType.title,
description: numberType.description
})
return definition
}
Expand Down Expand Up @@ -277,6 +287,8 @@ export type NumberDefinition = {
exclusiveMaximum?: number;
multipleOf?: number;
default?: number;
title?: string;
description?: string;
}

/**
Expand All @@ -285,6 +297,8 @@ export type NumberDefinition = {
export type BooleanDefinition = {
type: 'boolean';
default?: boolean;
title?: string;
description?: string;
}

export type ObjectDefinition = {
Expand All @@ -296,6 +310,8 @@ export type ObjectDefinition = {
maxProperties?: number,
anyOf?: Definition[],
default?: any
title?: string;
description?: string;
}

export type ArrayDefinition = {
Expand All @@ -305,6 +321,8 @@ export type ArrayDefinition = {
minItems?: number,
maxItems?: number,
default?: any[]
title?: string;
description?: string;
}

export type UndefinedDefinition = {
Expand All @@ -325,6 +343,8 @@ export type StringDefinition = {
maxLength?: number;
pattern?: string;
default?: string;
title?: string;
description?: string;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ export class Parser {
if (propertyJsDoc.comment) {
if (propertyJsDoc.name === 'default') {
type.default = this.getJsDocComment(propertyJsDoc.comment).toLowerCase() === 'true'
} else if (propertyJsDoc.name === 'title') {
type.title = propertyJsDoc.comment
} else if (propertyJsDoc.name === 'description') {
type.description = propertyJsDoc.comment
}
}
}
Expand All @@ -762,6 +766,10 @@ export class Parser {
type.pattern = propertyJsDoc.comment
} else if (propertyJsDoc.name === 'default') {
type.default = this.getJsDocComment(propertyJsDoc.comment)
} else if (propertyJsDoc.name === 'title') {
type.title = propertyJsDoc.comment
} else if (propertyJsDoc.name === 'description') {
type.description = propertyJsDoc.comment
}
}
}
Expand All @@ -780,6 +788,10 @@ export class Parser {
type.exclusiveMinimum = +propertyJsDoc.comment
} else if (propertyJsDoc.name === 'default') {
type.default = +this.getJsDocComment(propertyJsDoc.comment)
} else if (propertyJsDoc.name === 'title') {
type.title = propertyJsDoc.comment
} else if (propertyJsDoc.name === 'description') {
type.description = propertyJsDoc.comment
}
}
}
Expand Down Expand Up @@ -820,6 +832,10 @@ export class Parser {
}
} else if (jsDoc.name === 'uniqueItems') {
type.uniqueItems = true
} else if (jsDoc.name === 'title') {
type.title = jsDoc.comment
} else if (jsDoc.name === 'description') {
type.description = jsDoc.comment
}
}

Expand Down Expand Up @@ -863,6 +879,10 @@ export class Parser {
type.maxProperties = +jsDoc.comment
} else if (jsDoc.name === 'default') {
type.default = JSON.parse(this.getJsDocComment(jsDoc.comment))
} else if (jsDoc.name === 'title') {
type.title = jsDoc.comment
} else if (jsDoc.name === 'description') {
type.description = jsDoc.comment
}
} else {
if (jsDoc.name === 'additionalProperties') {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export type NumberType = {
multipleOf?: number;
default?: number;
enums?: string[];
title?: string;
description?: string;
}

/**
Expand All @@ -113,6 +115,8 @@ export type StringType = {
pattern?: string;
default?: string;
enums?: string[];
title?: string;
description?: string;
}

/**
Expand All @@ -121,6 +125,8 @@ export type StringType = {
export type BooleanType = {
kind: 'boolean';
default?: boolean;
title?: string;
description?: string;
}

/**
Expand All @@ -137,6 +143,8 @@ export type ObjectType = {
maxProperties?: number;
additionalProperties?: boolean | Type;
default?: any;
title?: string;
description?: string;
}

export type ArrayType = {
Expand All @@ -146,6 +154,8 @@ export type ArrayType = {
minItems?: number;
maxItems?: number;
default?: any[]
title?: string;
description?: string;
}

/**
Expand Down

0 comments on commit b89ac31

Please sign in to comment.