-
-
Notifications
You must be signed in to change notification settings - Fork 72
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give format precendence over type property #831
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
components: | ||
schemas: | ||
Body_upload_file_api_assets_post: | ||
properties: | ||
file: | ||
format: binary | ||
title: File | ||
type: string | ||
required: | ||
- file | ||
title: Body_upload_file_api_assets_post | ||
type: object | ||
$comment: | | ||
export type BodyUploadFileApiAssetsPost = { | ||
/** | ||
* @type string binary | ||
*/ | ||
file: Blob; | ||
}; | ||
Plain_file: | ||
format: binary | ||
title: File | ||
type: string | ||
$comment: export type PlainFile = Blob; | ||
Plain_date: | ||
format: date | ||
title: Date | ||
type: string | ||
$comment: export type PlainDate = Date; | ||
Plain_time: | ||
format: time | ||
title: Time | ||
type: number | ||
$comment: export type PlainTime = number; | ||
Plain_date_time: | ||
format: date-time | ||
title: Datetime | ||
type: string | ||
$comment: | ||
Plain_email: | ||
format: email | ||
title: Email | ||
type: string | ||
Plain_uuid: | ||
format: uuid | ||
title: UUID | ||
type: string | ||
info: | ||
title: Type Assertions | ||
openapi: 3.1.0 | ||
paths: {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,14 @@ | |
const schema = properties[name] as OasTypes.SchemaObject | ||
|
||
const isRequired = Array.isArray(required) ? required.includes(name) : !!required | ||
let type = this.getTypeFromSchema(schema, this.context.pluginManager.resolveName({ name: `${baseName || ''} ${name}`, pluginKey, type: 'type' })) | ||
let type = this.getTypeFromSchema( | ||
schema, | ||
this.context.pluginManager.resolveName({ | ||
name: `${baseName || ''} ${name}`, | ||
pluginKey, | ||
type: 'type', | ||
}), | ||
) | ||
|
||
if (!type) { | ||
return null | ||
|
@@ -303,7 +310,14 @@ | |
type: this.options.enumType, | ||
}), | ||
) | ||
return factory.createTypeReferenceNode(this.context.pluginManager.resolveName({ name: enumName, pluginKey, type: 'type' }), undefined) | ||
return factory.createTypeReferenceNode( | ||
this.context.pluginManager.resolveName({ | ||
name: enumName, | ||
pluginKey, | ||
type: 'type', | ||
}), | ||
undefined, | ||
) | ||
} | ||
|
||
if (schema.enum) { | ||
|
@@ -386,8 +400,47 @@ | |
}) | ||
} | ||
|
||
if (this.options.dateType === 'date' && ['date', 'date-time'].some((item) => item === schema.format)) { | ||
return factory.createTypeReferenceNode(factory.createIdentifier('Date')) | ||
/** | ||
* > Structural validation alone may be insufficient to allow an application to correctly utilize certain values. The "format" | ||
* > annotation keyword is defined to allow schema authors to convey semantic information for a fixed subset of values which are | ||
* > accurately described by authoritative resources, be they RFCs or other external specifications. | ||
* | ||
* In other words: format is more specific than type alone, hence it should override the type value, if possible. | ||
* | ||
* see also https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7 | ||
*/ | ||
if (schema.format) { | ||
switch (schema.format) { | ||
case 'binary': | ||
if (schema.type === 'string') { | ||
return factory.createTypeReferenceNode('Blob', []) | ||
} | ||
break | ||
// 7.3.1. Dates, Times, and Duration | ||
case 'date-time': | ||
case 'date': | ||
case 'time': | ||
if (this.options.dateType === 'date') { | ||
return factory.createTypeReferenceNode(factory.createIdentifier('Date')) | ||
} | ||
case 'uuid': | ||
// TODO how to work with this? use https://www.npmjs.com/package/uuid for it? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the end, it will be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thing is, the cases added here are directly from the openapi spec. That's why I added them here, too. Imo, being able to map to a specific type is very convinient. But kubb itself should add as little runtime dependencies as possible. Hence, uuid support via |
||
break | ||
case 'duration': | ||
case 'email': | ||
case 'idn-email': | ||
case 'hostname': | ||
case 'idn-hostname': | ||
case 'ipv4': | ||
case 'ipv6': | ||
case 'uri': | ||
case 'uri-reference': | ||
case 'json-pointer': | ||
case 'relative-json-pointer': | ||
default: | ||
// formats not yet implemented: ignore. | ||
break | ||
} | ||
} | ||
|
||
// string, boolean, null, number | ||
|
@@ -396,10 +449,6 @@ | |
} | ||
} | ||
|
||
if (schema.format === 'binary') { | ||
return factory.createTypeReferenceNode('Blob', []) | ||
} | ||
|
||
return this.#unknownReturn | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, im not sure if
time
should really be cast aDate
. Maybenumber
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
dateType
which can now be a date or a string so maybe we can also add number if that would be needed for other users.