11const R = require ( 'ramda' ) ;
2- const contentType = require ( 'content-type' ) ;
2+ const contentTyper = require ( 'content-type' ) ;
3+ const mediaTyper = require ( 'media-typer' ) ;
34const pipeParseResult = require ( '../../pipeParseResult' ) ;
45const {
56 isExtension, hasKey, getKey, getValue,
@@ -19,9 +20,52 @@ const unsupportedKeys = ['encoding'];
1920const isUnsupportedKey = R . anyPass ( R . map ( hasKey , unsupportedKeys ) ) ;
2021
2122function isJSONMediaType ( mediaType ) {
22- const type = contentType . parse ( mediaType ) ;
23- const jsonMediaType = / ^ a p p l i c a t i o n \/ \S * j s o n $ / i;
24- return jsonMediaType . test ( type . type ) ;
23+ const contentType = contentTyper . parse ( mediaType ) ;
24+ const { type, suffix, subtype } = mediaTyper . parse ( contentType . type ) ;
25+ return type === 'application' && ( suffix === 'json' || subtype === 'json' ) ;
26+ }
27+
28+ function isTextMediaType ( mediaType ) {
29+ const contentType = contentTyper . parse ( mediaType ) ;
30+ const { type } = mediaTyper . parse ( contentType . type ) ;
31+ return type === 'text' ;
32+ }
33+
34+ const canGenerateMessageBodyForMediaType = R . anyPass ( [ isJSONMediaType , isTextMediaType ] ) ;
35+
36+ function generateMessageBody ( context , mediaType , dataStructure ) {
37+ const elements = { } ;
38+ const { components } = context . state ;
39+ if ( components ) {
40+ const schemas = components . get ( 'schemas' ) ;
41+ if ( schemas ) {
42+ schemas . content
43+ . filter ( e => e . value && e . value . content )
44+ . forEach ( ( e ) => {
45+ const element = e . value . content ;
46+ elements [ element . id . toValue ( ) ] = element ;
47+ } ) ;
48+ }
49+ }
50+
51+ const value = dataStructure . content . valueOf ( undefined , elements ) ;
52+ if ( ! value ) {
53+ return undefined ;
54+ }
55+
56+ let body ;
57+ if ( isJSONMediaType ( mediaType ) ) {
58+ body = JSON . stringify ( value ) ;
59+ } else if ( isTextMediaType ( mediaType ) && typeof value === 'string' ) {
60+ body = value ;
61+ } else {
62+ return undefined ;
63+ }
64+
65+ const asset = new context . namespace . elements . Asset ( body ) ;
66+ asset . classes . push ( 'messageBody' ) ;
67+ asset . contentType = mediaType ;
68+ return asset ;
2569}
2670
2771const createJSONMessageBodyAsset = R . curry ( ( namespace , mediaType , value ) => {
@@ -46,7 +90,7 @@ const parseExampleObjectOrRef = parseReference('examples', parseExampleObject);
4690
4791const isValidMediaType = ( mediaType ) => {
4892 try {
49- contentType . parse ( mediaType . toValue ( ) ) ;
93+ contentTyper . parse ( mediaType . toValue ( ) ) ;
5094 } catch ( error ) {
5195 return false ;
5296 }
@@ -133,28 +177,9 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
133177
134178 const dataStructure = mediaTypeObject . get ( 'schema' ) ;
135179
136- if ( ! messageBody && dataStructure && isJSONMediaType ( mediaType ) ) {
137- const elements = { } ;
138- const { components } = context . state ;
139- if ( components ) {
140- const schemas = components . get ( 'schemas' ) ;
141- if ( schemas ) {
142- schemas . content
143- . filter ( e => e . value && e . value . content )
144- . forEach ( ( e ) => {
145- const element = e . value . content ;
146- elements [ element . id . toValue ( ) ] = element ;
147- } ) ;
148- }
149- }
150-
151- const value = dataStructure . content . valueOf ( undefined , elements ) ;
152-
153- if ( value ) {
154- const body = JSON . stringify ( value ) ;
155- const asset = new namespace . elements . Asset ( body ) ;
156- asset . classes . push ( 'messageBody' ) ;
157- asset . contentType = mediaType ;
180+ if ( ! messageBody && dataStructure && canGenerateMessageBodyForMediaType ( mediaType ) ) {
181+ const asset = generateMessageBody ( context , mediaType , dataStructure ) ;
182+ if ( asset ) {
158183 message . push ( asset ) ;
159184 }
160185 }
0 commit comments