-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat(oas): support application/x-www-form-urlencoded
OAS style serialization
#227
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { BodyConverter } from './BodyConverter'; | ||
import type { Sampler } from '../../Sampler'; | ||
import { FormUrlEncodedMediaTypeEncoder } from './encoders/FormUrlEncodedMediaTypeEncoder'; | ||
import type { OpenAPIV3, PostData } from '@har-sdk/core'; | ||
import { OpenAPIV2 } from '@har-sdk/core'; | ||
import pointer from 'json-pointer'; | ||
|
||
export class Oas3RequestBodyConverter extends BodyConverter<OpenAPIV3.Document> { | ||
private readonly formUrlEncodedEncoder = new FormUrlEncodedMediaTypeEncoder(); | ||
constructor(spec: OpenAPIV3.Document, sampler: Sampler) { | ||
super(spec, sampler); | ||
} | ||
|
@@ -43,6 +46,18 @@ export class Oas3RequestBodyConverter extends BodyConverter<OpenAPIV3.Document> | |
}); | ||
} | ||
|
||
protected encodeFormUrlencoded( | ||
value: unknown, | ||
fields?: Record<string, OpenAPIV3.EncodingObject>, | ||
schema?: OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject | ||
): string { | ||
return this.formUrlEncodedEncoder.encode({ | ||
fields, | ||
value, | ||
schema | ||
}); | ||
} | ||
|
||
private sampleAndEncodeRequestBody({ | ||
media, | ||
tokens, | ||
|
@@ -84,13 +99,15 @@ export class Oas3RequestBodyConverter extends BodyConverter<OpenAPIV3.Document> | |
Object.entries(mediaType.encoding ?? {}).map( | ||
([property, encoding]: [string, OpenAPIV3.EncodingObject]) => [ | ||
property, | ||
this.encodeValue({ | ||
value: data[property], | ||
contentType: encoding.contentType, | ||
schema: (mediaType.schema as OpenAPIV3.SchemaObject).properties[ | ||
property | ||
] | ||
}) | ||
encoding.contentType | ||
? this.encodeValue({ | ||
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 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. There is a possibility to encode some primitive or array to 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. Makes sense. However, you should figure out how to handle the |
||
value: data[property], | ||
contentType: encoding.contentType, | ||
schema: (mediaType.schema as OpenAPIV3.SchemaObject).properties[ | ||
property | ||
] | ||
}) | ||
: data[property] | ||
] | ||
) | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
OasStyleSerializer, | ||
OasStyleSerializerType | ||
} from '../style-serializers/OasStyleSerializer'; | ||
import { isObject } from '../../../../utils'; | ||
import { isPrimitive } from '../../../../utils/isPrimitive'; | ||
import { isArrayOfPrimitives } from '../../../../utils/isArrayOfPrimitives'; | ||
import { OasDeepObjectStyleSerializer } from '../style-serializers/OasDeepObjectStyleSerializer'; | ||
import { OasDefaultStylesSerializer } from '../style-serializers/OasDefaultStylesSerializer'; | ||
import { OpenAPIV2, type OpenAPIV3 } from '@har-sdk/core'; | ||
|
||
export class FormUrlEncodedMediaTypeEncoder { | ||
constructor( | ||
private readonly oasStyleSerializers: OasStyleSerializer[] = [ | ||
new OasDeepObjectStyleSerializer(), | ||
new OasDefaultStylesSerializer() | ||
] | ||
) {} | ||
|
||
public encode(encodingData: { | ||
value: unknown; | ||
fields?: Record<string, OpenAPIV3.EncodingObject>; | ||
schema?: OpenAPIV3.SchemaObject | OpenAPIV2.SchemaObject; | ||
}): string { | ||
const { value, fields } = encodingData; | ||
|
||
if (!isObject(value) || Array.isArray(value)) { | ||
return ''; | ||
} | ||
|
||
const entries = Object.entries(value) | ||
.map(([key, val]: [string, unknown]) => { | ||
const serializationParams = this.getSerializationParams( | ||
(fields ? fields[key] : undefined) ?? {} | ||
); | ||
|
||
const { style, explode, allowReserved } = serializationParams; | ||
|
||
const stringifyObject = | ||
style === OasStyleSerializerType.FORM && | ||
!(isPrimitive(val) || isArrayOfPrimitives(val)); | ||
|
||
return this.findSerializer(style)?.serialize( | ||
{ | ||
key, | ||
value: stringifyObject ? JSON.stringify(val) : val | ||
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. When dealing with complex values that are not arrays of primitive types or simple objects with primitive fields, you have to rely on the 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. Well, when 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 |
||
}, | ||
{ allowReserved, explode, style } | ||
); | ||
}) | ||
.filter(Boolean); | ||
|
||
return entries.join('&'); | ||
} | ||
|
||
private findSerializer(style: OasStyleSerializerType) { | ||
return this.oasStyleSerializers | ||
.filter((x) => x.supportsStyle(style)) | ||
.shift(); | ||
} | ||
|
||
private getSerializationParams(encodingObject: OpenAPIV3.EncodingObject) { | ||
const [encodingObjectStyle]: OasStyleSerializerType[] = Object.values( | ||
OasStyleSerializerType | ||
).filter((x) => x === encodingObject.style); | ||
|
||
const style = encodingObjectStyle ?? OasStyleSerializerType.FORM; | ||
|
||
return { | ||
style, | ||
explode: encodingObject.explode ?? false, | ||
allowReserved: encodingObject.allowReserved ?? false, | ||
contentType: encodingObject.contentType | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
OasStyleSerializer, | ||
type OasStyleSerializerData, | ||
type OasStyleSerializerOptions, | ||
OasStyleSerializerType | ||
} from './OasStyleSerializer'; | ||
import { isPrimitive } from '../../../../utils/isPrimitive'; | ||
import { UriTemplator } from '../../UriTemplator'; | ||
import { decodeReserved } from '../../../../utils/decodeReserved'; | ||
|
||
export class OasDeepObjectStyleSerializer implements OasStyleSerializer { | ||
public supportsStyle(style: OasStyleSerializerType): boolean { | ||
return OasStyleSerializerType.DEEP_OBJECT === style; | ||
} | ||
|
||
public serialize( | ||
{ key, value }: OasStyleSerializerData, | ||
{ style, explode, allowReserved }: OasStyleSerializerOptions | ||
): string { | ||
if (!this.supportsStyle(style) || !this.shouldSerialize(value, explode)) { | ||
return ''; | ||
} | ||
|
||
const result = Object.entries(value).reduce( | ||
(acc, [prop, val]: [string, unknown]) => { | ||
const keyValue = new UriTemplator() | ||
.substitute(`?${key}[${prop}]={val}`, { | ||
val: val ?? '' | ||
}) | ||
.substring(1); | ||
|
||
if (keyValue.length) { | ||
const prefix = acc ? `${acc}&` : ''; | ||
|
||
return `${prefix}${keyValue}`; | ||
} | ||
|
||
return acc; | ||
}, | ||
'' | ||
); | ||
|
||
return allowReserved ? decodeReserved(result) : result; | ||
} | ||
|
||
private shouldSerialize(value: unknown, explode: boolean): value is object { | ||
return !!value && explode && !Array.isArray(value) && !isPrimitive(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
OasStyleSerializer, | ||
type OasStyleSerializerData, | ||
type OasStyleSerializerOptions, | ||
OasStyleSerializerType | ||
} from './OasStyleSerializer'; | ||
import { isArrayOfPrimitives } from '../../../../utils/isArrayOfPrimitives'; | ||
import { UriTemplator } from '../../UriTemplator'; | ||
import { decodeReserved } from '../../../../utils/decodeReserved'; | ||
|
||
export class OasDefaultStylesSerializer implements OasStyleSerializer { | ||
private readonly CUSTOM_DELIMITERS: ReadonlyMap< | ||
OasStyleSerializerType, | ||
string | ||
> = new Map<OasStyleSerializerType, string>([ | ||
[OasStyleSerializerType.PIPE_DELIMITED, '|'], | ||
[OasStyleSerializerType.SPACE_DELIMITED, '%20'] | ||
]); | ||
|
||
public supportsStyle(style: OasStyleSerializerType): boolean { | ||
return [ | ||
OasStyleSerializerType.PIPE_DELIMITED, | ||
OasStyleSerializerType.SPACE_DELIMITED, | ||
OasStyleSerializerType.FORM | ||
].includes(style); | ||
} | ||
|
||
public serialize( | ||
{ key, value }: OasStyleSerializerData, | ||
{ explode, allowReserved, style }: OasStyleSerializerOptions | ||
): string { | ||
if ( | ||
!this.supportsStyle(style) || | ||
this.isNonArrayValueForArrayStyle(style, value) | ||
) { | ||
return ''; | ||
} | ||
|
||
const template = this.getTemplateString(key, explode, style); | ||
|
||
let result = new UriTemplator() | ||
.substitute(template, { | ||
[key]: value | ||
}) | ||
.substring(1); | ||
|
||
result = this.replaceDelimiter(style, result); | ||
|
||
return allowReserved ? decodeReserved(result) : result; | ||
} | ||
|
||
private isNonArrayValueForArrayStyle( | ||
style: OasStyleSerializerType, | ||
value: unknown | ||
) { | ||
return ( | ||
[ | ||
OasStyleSerializerType.PIPE_DELIMITED, | ||
OasStyleSerializerType.SPACE_DELIMITED | ||
].includes(style) && !isArrayOfPrimitives(value) | ||
); | ||
} | ||
|
||
private replaceDelimiter(style: OasStyleSerializerType, result: string) { | ||
const customDelimiter = this.CUSTOM_DELIMITERS.get(style); | ||
|
||
return customDelimiter ? result.replace(/,/g, customDelimiter) : result; | ||
} | ||
|
||
private getTemplateString( | ||
key: string, | ||
explode: boolean, | ||
style?: OasStyleSerializerType | ||
) { | ||
const suffix = explode && style === OasStyleSerializerType.FORM ? '*' : ''; | ||
|
||
return `{?${key}${suffix}}`; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export enum OasStyleSerializerType { | ||
FORM = 'form', | ||
SPACE_DELIMITED = 'spaceDelimited', | ||
PIPE_DELIMITED = 'pipeDelimited', | ||
DEEP_OBJECT = 'deepObject' | ||
} | ||
|
||
export interface OasStyleSerializerData { | ||
key: string; | ||
value: unknown; | ||
} | ||
|
||
export interface OasStyleSerializerOptions { | ||
explode: boolean; | ||
allowReserved: boolean; | ||
style: OasStyleSerializerType; | ||
} | ||
|
||
export interface OasStyleSerializer { | ||
supportsStyle(style: OasStyleSerializerType): boolean; | ||
|
||
serialize( | ||
serializerData: OasStyleSerializerData, | ||
serializerOptions: OasStyleSerializerOptions | ||
): string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const specialChars = new Map( | ||
Array.from(":/?#[]@!$&'()*+,;=").map((c) => [c.charCodeAt(0), c]) | ||
); | ||
|
||
export const decodeReserved = (val: string) => | ||
val.replace(/%[0-9A-Fa-f]{2}/g, (match) => { | ||
const decodedChar = specialChars.get(parseInt(match.substring(1), 16)); | ||
|
||
return decodedChar ? decodedChar : match; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { isPrimitive } from './isPrimitive'; | ||
|
||
export const isArrayOfPrimitives = (value: unknown): boolean => | ||
Array.isArray(value) && value.every((item: unknown) => isPrimitive(item)); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||
export const isPrimitive = ( | ||||||
value: unknown | ||||||
): value is null | undefined | string | boolean | number | bigint | symbol => | ||||||
// ADHOC: typeof null === 'object' | ||||||
value === null || | ||||||
value === undefined || | ||||||
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.
Suggested change
|
||||||
(typeof value !== 'object' && typeof value !== 'function'); |
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.
Convert args to an object and introduce interface