Skip to content
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

Generalized mime type support #17285

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ let primitives = [
"any"
];

const supportedMediaTypes: { [mediaType: string]: number } = {
"application/json": Infinity,
"application/json-patch+json": 1,
"application/merge-patch+json": 1,
"application/strategic-merge-patch+json": 1,
"application/octet-stream": 0,
"application/x-www-form-urlencoded": 0
}


let enumsMap: Set<string> = new Set<string>([
{{#models}}
{{#model}}
Expand Down Expand Up @@ -59,6 +49,45 @@ let typeMap: {[index: string]: any} = {
{{/models}}
}

type MimeTypeDescriptor = {
a-ignatov-parc marked this conversation as resolved.
Show resolved Hide resolved
type: string;
subtype: string;
subtypeTokens: string[];
};

const parseMimeType = (mimeType: string): MimeTypeDescriptor => {
const [type, subtype] = mimeType.split('/');
return {
type,
subtype,
subtypeTokens: subtype.split('+'),
};
};

type MimeTypePredicate = (mimeType: string) => boolean;

const mimeTypePredicateFactory = (predicate: (descriptor: MimeTypeDescriptor) => boolean): MimeTypePredicate => (mimeType) => predicate(parseMimeType(mimeType));

const mimeTypeSimplePredicateFactory = (type: string, subtype?: string): MimeTypePredicate => mimeTypePredicateFactory((descriptor) => {
if (descriptor.type !== type) return false;
if (subtype != null && descriptor.subtype !== subtype) return false;
return true;
});

const isTextLikeMimeType = mimeTypeSimplePredicateFactory('text');
const isJsonMimeType = mimeTypeSimplePredicateFactory('application', 'json');
const isJsonLikeMimeType = mimeTypePredicateFactory((descriptor) => descriptor.type === 'application' && descriptor.subtypeTokens.includes('json'));
const isOctetStreamMimeType = mimeTypeSimplePredicateFactory('application', 'octet-stream');
const isFormUrlencodedMimeType = mimeTypeSimplePredicateFactory('application', 'x-www-form-urlencoded');

const supportedMimeTypePredicatesWithPriority: MimeTypePredicate[] = [
a-ignatov-parc marked this conversation as resolved.
Show resolved Hide resolved
isJsonMimeType,
isJsonLikeMimeType,
isTextLikeMimeType,
isOctetStreamMimeType,
isFormUrlencodedMimeType,
];

export class ObjectSerializer {
public static findCorrectType(data: any, expectedType: string) {
if (data == undefined) {
Expand Down Expand Up @@ -204,14 +233,15 @@ export class ObjectSerializer {
}

const normalMediaTypes = mediaTypes.map(this.normalizeMediaType);
let selectedMediaType: string | undefined = undefined;
let selectedRank: number = -Infinity;
for (const mediaType of normalMediaTypes) {
if (supportedMediaTypes[mediaType!] > selectedRank) {
selectedMediaType = mediaType;
selectedRank = supportedMediaTypes[mediaType!];
}
}

const supportedAndOrderedMediaTypes = supportedMimeTypePredicatesWithPriority.reduce<string[]>((result, predicate) => {
a-ignatov-parc marked this conversation as resolved.
Show resolved Hide resolved
const filteredMediaTypes = normalMediaTypes.filter((mediaType): mediaType is Exclude<typeof mediaType, undefined> => {
return mediaType != null && predicate(mediaType);
});
return result.concat(filteredMediaTypes);
}, []);

const selectedMediaType: string | undefined = supportedAndOrderedMediaTypes[0];

if (selectedMediaType === undefined) {
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
Expand All @@ -224,11 +254,11 @@ export class ObjectSerializer {
* Convert data to a string according the given media type
*/
public static stringify(data: any, mediaType: string): string {
if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return String(data);
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.stringify(data);
}

Expand All @@ -243,18 +273,14 @@ export class ObjectSerializer {
throw new Error("Cannot parse content. No Content-Type defined.");
}

if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return rawData;
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.parse(rawData);
}

if (mediaType === "text/html") {
return rawData;
}

throw new Error("The mediaType " + mediaType + " is not supported by ObjectSerializer.parse.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,52 @@ let primitives = [
"any"
];

const supportedMediaTypes: { [mediaType: string]: number } = {
"application/json": Infinity,
"application/json-patch+json": 1,
"application/merge-patch+json": 1,
"application/strategic-merge-patch+json": 1,
"application/octet-stream": 0,
"application/x-www-form-urlencoded": 0
}


let enumsMap: Set<string> = new Set<string>([
]);

let typeMap: {[index: string]: any} = {
"Response": Response,
}

type MimeTypeDescriptor = {
type: string;
subtype: string;
subtypeTokens: string[];
};

const parseMimeType = (mimeType: string): MimeTypeDescriptor => {
const [type, subtype] = mimeType.split('/');
return {
type,
subtype,
subtypeTokens: subtype.split('+'),
};
};

type MimeTypePredicate = (mimeType: string) => boolean;

const mimeTypePredicateFactory = (predicate: (descriptor: MimeTypeDescriptor) => boolean): MimeTypePredicate => (mimeType) => predicate(parseMimeType(mimeType));

const mimeTypeSimplePredicateFactory = (type: string, subtype?: string): MimeTypePredicate => mimeTypePredicateFactory((descriptor) => {
if (descriptor.type !== type) return false;
if (subtype != null && descriptor.subtype !== subtype) return false;
return true;
});

const isTextLikeMimeType = mimeTypeSimplePredicateFactory('text');
const isJsonMimeType = mimeTypeSimplePredicateFactory('application', 'json');
const isJsonLikeMimeType = mimeTypePredicateFactory((descriptor) => descriptor.type === 'application' && descriptor.subtypeTokens.includes('json'));
const isOctetStreamMimeType = mimeTypeSimplePredicateFactory('application', 'octet-stream');
const isFormUrlencodedMimeType = mimeTypeSimplePredicateFactory('application', 'x-www-form-urlencoded');

const supportedMimeTypePredicatesWithPriority: MimeTypePredicate[] = [
isJsonMimeType,
isJsonLikeMimeType,
isTextLikeMimeType,
isOctetStreamMimeType,
isFormUrlencodedMimeType,
];

export class ObjectSerializer {
public static findCorrectType(data: any, expectedType: string) {
if (data == undefined) {
Expand Down Expand Up @@ -176,14 +205,15 @@ export class ObjectSerializer {
}

const normalMediaTypes = mediaTypes.map(this.normalizeMediaType);
let selectedMediaType: string | undefined = undefined;
let selectedRank: number = -Infinity;
for (const mediaType of normalMediaTypes) {
if (supportedMediaTypes[mediaType!] > selectedRank) {
selectedMediaType = mediaType;
selectedRank = supportedMediaTypes[mediaType!];
}
}

const supportedAndOrderedMediaTypes = supportedMimeTypePredicatesWithPriority.reduce<string[]>((result, predicate) => {
const filteredMediaTypes = normalMediaTypes.filter((mediaType): mediaType is Exclude<typeof mediaType, undefined> => {
return mediaType != null && predicate(mediaType);
});
return result.concat(filteredMediaTypes);
}, []);

const selectedMediaType: string | undefined = supportedAndOrderedMediaTypes[0];

if (selectedMediaType === undefined) {
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
Expand All @@ -196,11 +226,11 @@ export class ObjectSerializer {
* Convert data to a string according the given media type
*/
public static stringify(data: any, mediaType: string): string {
if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return String(data);
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.stringify(data);
}

Expand All @@ -215,18 +245,14 @@ export class ObjectSerializer {
throw new Error("Cannot parse content. No Content-Type defined.");
}

if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return rawData;
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.parse(rawData);
}

if (mediaType === "text/html") {
return rawData;
}

throw new Error("The mediaType " + mediaType + " is not supported by ObjectSerializer.parse.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ let primitives = [
"any"
];

const supportedMediaTypes: { [mediaType: string]: number } = {
"application/json": Infinity,
"application/json-patch+json": 1,
"application/merge-patch+json": 1,
"application/strategic-merge-patch+json": 1,
"application/octet-stream": 0,
"application/x-www-form-urlencoded": 0
}


let enumsMap: Set<string> = new Set<string>([
"OrderStatusEnum",
"PetStatusEnum",
Expand All @@ -48,6 +38,45 @@ let typeMap: {[index: string]: any} = {
"User": User,
}

type MimeTypeDescriptor = {
type: string;
subtype: string;
subtypeTokens: string[];
};

const parseMimeType = (mimeType: string): MimeTypeDescriptor => {
const [type, subtype] = mimeType.split('/');
return {
type,
subtype,
subtypeTokens: subtype.split('+'),
};
};

type MimeTypePredicate = (mimeType: string) => boolean;

const mimeTypePredicateFactory = (predicate: (descriptor: MimeTypeDescriptor) => boolean): MimeTypePredicate => (mimeType) => predicate(parseMimeType(mimeType));

const mimeTypeSimplePredicateFactory = (type: string, subtype?: string): MimeTypePredicate => mimeTypePredicateFactory((descriptor) => {
if (descriptor.type !== type) return false;
if (subtype != null && descriptor.subtype !== subtype) return false;
return true;
});

const isTextLikeMimeType = mimeTypeSimplePredicateFactory('text');
const isJsonMimeType = mimeTypeSimplePredicateFactory('application', 'json');
const isJsonLikeMimeType = mimeTypePredicateFactory((descriptor) => descriptor.type === 'application' && descriptor.subtypeTokens.includes('json'));
const isOctetStreamMimeType = mimeTypeSimplePredicateFactory('application', 'octet-stream');
const isFormUrlencodedMimeType = mimeTypeSimplePredicateFactory('application', 'x-www-form-urlencoded');

const supportedMimeTypePredicatesWithPriority: MimeTypePredicate[] = [
isJsonMimeType,
isJsonLikeMimeType,
isTextLikeMimeType,
isOctetStreamMimeType,
isFormUrlencodedMimeType,
];

export class ObjectSerializer {
public static findCorrectType(data: any, expectedType: string) {
if (data == undefined) {
Expand Down Expand Up @@ -193,14 +222,15 @@ export class ObjectSerializer {
}

const normalMediaTypes = mediaTypes.map(this.normalizeMediaType);
let selectedMediaType: string | undefined = undefined;
let selectedRank: number = -Infinity;
for (const mediaType of normalMediaTypes) {
if (supportedMediaTypes[mediaType!] > selectedRank) {
selectedMediaType = mediaType;
selectedRank = supportedMediaTypes[mediaType!];
}
}

const supportedAndOrderedMediaTypes = supportedMimeTypePredicatesWithPriority.reduce<string[]>((result, predicate) => {
const filteredMediaTypes = normalMediaTypes.filter((mediaType): mediaType is Exclude<typeof mediaType, undefined> => {
return mediaType != null && predicate(mediaType);
});
return result.concat(filteredMediaTypes);
}, []);

const selectedMediaType: string | undefined = supportedAndOrderedMediaTypes[0];

if (selectedMediaType === undefined) {
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
Expand All @@ -213,11 +243,11 @@ export class ObjectSerializer {
* Convert data to a string according the given media type
*/
public static stringify(data: any, mediaType: string): string {
if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return String(data);
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.stringify(data);
}

Expand All @@ -232,18 +262,14 @@ export class ObjectSerializer {
throw new Error("Cannot parse content. No Content-Type defined.");
}

if (mediaType === "text/plain") {
if (isTextLikeMimeType(mediaType)) {
return rawData;
}

if (mediaType === "application/json" || mediaType === "application/json-patch+json" || mediaType === "application/merge-patch+json" || mediaType === "application/strategic-merge-patch+json") {
if (isJsonLikeMimeType(mediaType)) {
return JSON.parse(rawData);
}

if (mediaType === "text/html") {
return rawData;
}

throw new Error("The mediaType " + mediaType + " is not supported by ObjectSerializer.parse.");
}
}
Loading
Loading