11import { logger } from "../../core/logger"
22import type {
3+ IRMediaType ,
34 IROperation ,
45 IRParameter ,
56} from "../../core/openapi-types-normalized"
@@ -145,12 +146,45 @@ export type RequestBodyAsParameter = {
145146 contentType : string
146147}
147148
149+ export function filterBySupportedMediaTypes (
150+ object : Record < string , IRMediaType > ,
151+ supportedMediaTypes : string [ ] ,
152+ ) : { contentType : string ; mediaType : IRMediaType } [ ] {
153+ const normalized = Object . fromEntries (
154+ Object . entries ( object ) . map ( ( [ key , value ] ) => {
155+ const contentType = key . split ( / \s * [ , ; ] \s * / ) [ 0 ]
156+
157+ if ( ! contentType ) {
158+ throw new Error ( `unspecified content type '${ key } '` )
159+ }
160+
161+ return [ contentType , { fullContentType : key , mediaType : value } ]
162+ } ) ,
163+ )
164+
165+ return supportedMediaTypes
166+ . map ( ( supportedMediaType ) => normalized [ supportedMediaType ] )
167+ . filter ( isDefined )
168+ . map ( ( it ) => ( {
169+ contentType : it . fullContentType ,
170+ mediaType : it . mediaType ,
171+ } ) )
172+ }
173+
174+ export function firstByBySupportedMediaTypes (
175+ object : Record < string , IRMediaType > ,
176+ supportedMediaTypes : string [ ] ,
177+ ) : { contentType : string ; mediaType : IRMediaType } | undefined {
178+ return filterBySupportedMediaTypes ( object , supportedMediaTypes ) [ 0 ]
179+ }
180+
148181export function requestBodyAsParameter (
149182 operation : IROperation ,
150183 supportedMediaTypes = [
151184 "application/json" ,
152- "text/ json" ,
185+ "application/scim+ json" ,
153186 "application/merge-patch+json" ,
187+ "text/json" ,
154188 ] ,
155189) : RequestBodyAsParameter | undefined {
156190 const { requestBody} = operation
@@ -159,52 +193,41 @@ export function requestBodyAsParameter(
159193 return undefined
160194 }
161195
162- const normalized = Object . entries ( requestBody . content ) . map ( ( [ key , value ] ) => {
163- const contentType = key . split ( / \s * [ , ; ] \s * / ) [ 0 ]
196+ // todo: support multiple media types properly. https://github.com/mnahkies/openapi-code-generator/issues/42
197+ const result = firstByBySupportedMediaTypes (
198+ requestBody . content ,
199+ supportedMediaTypes ,
200+ )
164201
165- if ( ! contentType ) {
166- throw new Error ( `unspecified content type '${ key } '` )
167- }
168-
169- return { contentType, fullContentType : key , value}
170- } )
171-
172- // todo: https://github.com/mnahkies/openapi-code-generator/issues/42
173- for ( const supportedMediaType of supportedMediaTypes ) {
174- const result = normalized . find (
175- ( it ) => it . contentType === supportedMediaType ,
176- )
177-
178- if ( result ) {
179- return {
180- isSupported : true ,
181- contentType : result . fullContentType ,
182- parameter : {
183- name : "requestBody" ,
184- description : requestBody . description ,
185- in : "body" ,
186- required : requestBody . required ,
187- schema : result . value . schema ,
188- allowEmptyValue : false ,
189- deprecated : false ,
190- } ,
191- }
202+ if ( result ) {
203+ return {
204+ isSupported : true ,
205+ contentType : result . contentType ,
206+ parameter : {
207+ name : "requestBody" ,
208+ description : requestBody . description ,
209+ in : "body" ,
210+ required : requestBody . required ,
211+ schema : result . mediaType . schema ,
212+ allowEmptyValue : false ,
213+ deprecated : false ,
214+ } ,
192215 }
193216 }
194217
195218 logger . warn ( "no supported content-type on defined request body " , {
196219 requestBody,
197220 } )
198221
199- const first = normalized [ 0 ]
222+ const contentType = Object . keys ( requestBody . content ) . sort ( ) . join ( ", " )
200223
201- if ( ! first ) {
224+ if ( ! contentType ) {
202225 return undefined
203226 }
204227
205228 return {
206229 isSupported : false ,
207- contentType : first . fullContentType ,
230+ contentType,
208231 parameter : {
209232 name : "requestBody" ,
210233 description : requestBody . description ,
0 commit comments