@@ -43,13 +43,6 @@ const standardMediaTypes = {
4343 'text/xml' : 'TEXT_XML'
4444}
4545
46- const jsonMimeTypes = [
47- 'application/json' ,
48- 'text/json' ,
49- 'text/x-json' ,
50- 'application/x-json'
51- ]
52-
5346const multipartMimeTypes = [
5447 'multipart/form-data' ,
5548 'multipart/mixed' ,
@@ -63,46 +56,59 @@ module.exports = function (source, options) {
6356 entityType : 'String'
6457 } , options )
6558
59+ const state = {
60+ bodyType : null
61+ }
62+
6663 const code = new CodeBuilder ( opts . indent )
6764
6865 code . push ( 'RestClient restClient = RestClient.create();' )
6966 . blank ( )
7067
71- if ( source . postData && source . postData . mimeType === 'application/x-www-form-urlencoded' && source . postData . params ) {
72- code . push ( 'MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();' )
73- source . postData . params . forEach ( function ( param ) {
74- code . push ( 'formDataMap.add("%qd", "%qd");' , param . name , param . value )
75- } )
76- code . blank ( )
77- }
68+ if ( source . postData ) {
69+ if ( source . postData . params && source . postData . mimeType === 'application/x-www-form-urlencoded' ) {
70+ state . bodyType = 'form'
7871
79- if ( source . postData && multipartMimeTypes . includes ( source . postData . mimeType ) && source . postData . params ) {
80- code . push ( 'MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();' )
72+ code . push ( 'MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();' )
8173
82- source . postData . params . forEach ( function ( param ) {
83- if ( param . fileName ) {
84- if ( param . value ) {
85- code . push ( 'multipartBuilder.part("%s", "%qd")' , param . name , param . value )
86- code . push ( 1 , '.filename("%s")' , param . fileName )
87- } else {
88- code . push ( 'multipartBuilder.part("%s", new FileSystemResource("%s"))' , param . name , param . fileName )
89- }
74+ source . postData . params . forEach ( function ( param ) {
75+ code . push ( 'formDataMap.add("%qd", "%qd");' , param . name , param . value )
76+ } )
77+
78+ code . blank ( )
79+ } else if ( source . postData . params && multipartMimeTypes . includes ( source . postData . mimeType ) ) {
80+ state . bodyType = 'multipart'
9081
91- if ( param . contentType ) {
92- const mediaTypeConstant = standardMediaTypes [ param . contentType ]
93- if ( mediaTypeConstant ) {
94- code . push ( 1 , '.contentType(MediaType.%s);' , mediaTypeConstant )
82+ code . push ( 'MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();' )
83+
84+ source . postData . params . forEach ( function ( param ) {
85+ if ( param . fileName ) {
86+ if ( param . value ) {
87+ code . push ( 'multipartBuilder.part("%s", "%qd")' , param . name , param . value )
88+ code . push ( 1 , '.filename("%s")' , param . fileName )
9589 } else {
96- code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"));' , param . contentType )
90+ code . push ( 'multipartBuilder.part("%s", new FileSystemResource("%s"))' , param . name , param . fileName )
91+ }
92+
93+ if ( param . contentType ) {
94+ const mediaTypeConstant = standardMediaTypes [ param . contentType ]
95+ if ( mediaTypeConstant ) {
96+ code . push ( 1 , '.contentType(MediaType.%s);' , mediaTypeConstant )
97+ } else {
98+ code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"));' , param . contentType )
99+ }
100+ } else {
101+ code . push ( 1 , ';' )
97102 }
98103 } else {
99- code . push ( 1 , '; ')
104+ code . push ( 'multipartBuilder.part("%s", "%qd");' , param . name , param . value || ' ')
100105 }
101- } else {
102- code . push ( 'multipartBuilder.part("%s", "%qd");' , param . name , param . value || '' )
103- }
104- } )
105- code . blank ( )
106+ } )
107+
108+ code . blank ( )
109+ } else if ( source . postData . text ) {
110+ state . bodyType = 'plaintext'
111+ }
106112 }
107113
108114 code . push ( 'ResponseEntity<%s> response = restClient' , opts . entityType )
@@ -146,23 +152,22 @@ module.exports = function (source, options) {
146152 } )
147153 }
148154
149- if ( source . postData && ( source . postData . params || source . postData . text ) ) {
150- const mediaTypeEnumConstant = standardMediaTypes [ source . postData . mimeType ]
151-
152- if ( mediaTypeEnumConstant ) {
153- code . push ( 1 , '.contentType(MediaType.%s)' , mediaTypeEnumConstant )
154- } else {
155- code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"))' , source . postData . mimeType )
155+ if ( source . postData && state . bodyType ) {
156+ if ( source . postData . mimeType ) {
157+ const mediaTypeEnumConstant = standardMediaTypes [ source . postData . mimeType ]
158+ if ( mediaTypeEnumConstant ) {
159+ code . push ( 1 , '.contentType(MediaType.%s)' , mediaTypeEnumConstant )
160+ } else {
161+ code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"))' , source . postData . mimeType )
162+ }
156163 }
157164
158- if ( source . postData . mimeType === 'application/x-www- form-urlencoded' && source . postData . params ) {
165+ if ( state . bodyType === 'form' ) {
159166 code . push ( 1 , '.body(formDataMap)' )
160- } else if ( multipartMimeTypes . includes ( source . postData . mimeType ) && source . postData . params ) {
167+ } else if ( state . bodyType === 'multipart' ) {
161168 code . push ( 1 , '.body(multipartBuilder.build())' )
162- } else if ( source . postData . text ) {
163- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
164- } else if ( source . postData . jsonObj && jsonMimeTypes . includes ( source . postData . mimeType ) ) {
165- code . push ( 1 , '.body(%s)' , JSON . stringify ( JSON . stringify ( source . postData . jsonObj ) ) )
169+ } else if ( state . bodyType === 'plaintext' ) {
170+ code . push ( 1 , '.body("%qd")' , source . postData . text )
166171 }
167172 }
168173
0 commit comments