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 all 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,58 @@ 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[];
};

/**
* Every mime-type consists of a type, subtype, and optional parameters.
* The subtype can be composite, including information about the content format.
* For example: `application/json-patch+json`, `application/merge-patch+json`.
*
* This helper transforms a string mime-type into an internal representation.
* This simplifies the implementation of predicates that in turn define common rules for parsing or stringifying
* the payload.
*/
const parseMimeType = (mimeType: string): MimeTypeDescriptor => {
const [type, subtype] = mimeType.split('/');
return {
type,
subtype,
subtypeTokens: subtype.split('+'),
};
};

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

// This factory creates a predicate function that checks a string mime-type against defined rules.
const mimeTypePredicateFactory = (predicate: (descriptor: MimeTypeDescriptor) => boolean): MimeTypePredicate => (mimeType) => predicate(parseMimeType(mimeType));

// Use this factory when you need to define a simple predicate based only on type and, if applicable, subtype.
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;
});

// Creating a set of named predicates that will help us determine how to handle different mime-types
const isTextLikeMimeType = mimeTypeSimplePredicateFactory('text');
const isJsonMimeType = mimeTypeSimplePredicateFactory('application', 'json');
const isJsonLikeMimeType = mimeTypePredicateFactory((descriptor) => descriptor.type === 'application' && descriptor.subtypeTokens.some((item) => item === 'json'));
const isOctetStreamMimeType = mimeTypeSimplePredicateFactory('application', 'octet-stream');
const isFormUrlencodedMimeType = mimeTypeSimplePredicateFactory('application', 'x-www-form-urlencoded');

// Defining a list of mime-types in the order of prioritization for handling.
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,31 +246,27 @@ 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!];
}
}

if (selectedMediaType === undefined) {
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
for (const predicate of supportedMimeTypePredicatesWithPriority) {
for (const mediaType of normalMediaTypes) {
if (mediaType != null && predicate(mediaType)) {
return mediaType;
}
}
}

return selectedMediaType!;
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
}

/**
* 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 +281,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,65 @@ 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[];
};

/**
* Every mime-type consists of a type, subtype, and optional parameters.
* The subtype can be composite, including information about the content format.
* For example: `application/json-patch+json`, `application/merge-patch+json`.
*
* This helper transforms a string mime-type into an internal representation.
* This simplifies the implementation of predicates that in turn define common rules for parsing or stringifying
* the payload.
*/
const parseMimeType = (mimeType: string): MimeTypeDescriptor => {
const [type, subtype] = mimeType.split('/');
return {
type,
subtype,
subtypeTokens: subtype.split('+'),
};
};

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

// This factory creates a predicate function that checks a string mime-type against defined rules.
const mimeTypePredicateFactory = (predicate: (descriptor: MimeTypeDescriptor) => boolean): MimeTypePredicate => (mimeType) => predicate(parseMimeType(mimeType));

// Use this factory when you need to define a simple predicate based only on type and, if applicable, subtype.
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;
});

// Creating a set of named predicates that will help us determine how to handle different mime-types
const isTextLikeMimeType = mimeTypeSimplePredicateFactory('text');
const isJsonMimeType = mimeTypeSimplePredicateFactory('application', 'json');
const isJsonLikeMimeType = mimeTypePredicateFactory((descriptor) => descriptor.type === 'application' && descriptor.subtypeTokens.some((item) => item === 'json'));
const isOctetStreamMimeType = mimeTypeSimplePredicateFactory('application', 'octet-stream');
const isFormUrlencodedMimeType = mimeTypeSimplePredicateFactory('application', 'x-www-form-urlencoded');

// Defining a list of mime-types in the order of prioritization for handling.
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,31 +218,27 @@ 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!];
}
}

if (selectedMediaType === undefined) {
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
for (const predicate of supportedMimeTypePredicatesWithPriority) {
for (const mediaType of normalMediaTypes) {
if (mediaType != null && predicate(mediaType)) {
return mediaType;
}
}
}

return selectedMediaType!;
throw new Error("None of the given media types are supported: " + mediaTypes.join(", "));
}

/**
* 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 +253,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