File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ // tslint:disable:no-submodule-imports
2
+ import { Expose } from 'class-transformer'
3
+ import { IsString } from 'class-validator'
4
+ import { validationMetadatasToSchemas } from '../src'
5
+ const { defaultMetadataStorage } = require ( 'class-transformer/cjs/storage' )
6
+
7
+ // @ts -ignore unused
8
+ class User {
9
+ @IsString ( )
10
+ id : string
11
+
12
+ @Expose ( { name : 'domain_id' } )
13
+ @IsString ( )
14
+ domainId : string
15
+ }
16
+
17
+ describe ( 'Expose() decorator' , ( ) => {
18
+ it ( 'rename Expose()-decorated properties from output schema' , ( ) => {
19
+ const schema = validationMetadatasToSchemas ( {
20
+ classTransformerMetadataStorage : defaultMetadataStorage ,
21
+ } )
22
+
23
+ expect ( schema ) . toEqual ( {
24
+ User : {
25
+ properties : {
26
+ id : { type : 'string' } ,
27
+ domain_id : { type : 'string' } ,
28
+ } ,
29
+ type : 'object' ,
30
+ required : [ 'id' , 'domain_id' ] ,
31
+ } ,
32
+ } )
33
+ } )
34
+ } )
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import type { ReferenceObject, SchemaObject } from 'openapi3-ts'
8
8
import { getMetadataSchema } from './decorators'
9
9
import { defaultConverters } from './defaultConverters'
10
10
import { defaultOptions , IOptions } from './options'
11
+ import { ExposeMetadata } from 'class-transformer'
11
12
12
13
export { JSONSchema } from './decorators'
13
14
@@ -64,6 +65,26 @@ export function validationMetadataArrayToSchemas(
64
65
isExcluded ( { ...propMeta , target } , options )
65
66
)
66
67
)
68
+ . map ( ( propMeta ) => {
69
+ /**
70
+ * Retrieves all properties that have the Expose decorator from class-transformer
71
+ * and remaps the property names to the names exposed by the Expose decorator.
72
+ */
73
+ const exposeMetadata =
74
+ userOptions ?. classTransformerMetadataStorage ?. getExposedMetadatas (
75
+ propMeta . target as any
76
+ )
77
+
78
+ const ctMetaForField = exposeMetadata ?. find (
79
+ ( meta : ExposeMetadata ) => meta . propertyName === propMeta . propertyName
80
+ )
81
+
82
+ if ( ctMetaForField ?. options . name ) {
83
+ propMeta . propertyName = ctMetaForField . options . name
84
+ }
85
+
86
+ return propMeta
87
+ } )
67
88
68
89
const properties : { [ name : string ] : ReferenceObject | SchemaObject } = { }
69
90
You can’t perform that action at this time.
0 commit comments