Skip to content

Commit

Permalink
Generalized mime type support (#17285)
Browse files Browse the repository at this point in the history
* implemented generalized content-type handling

* regenerated samples

* addressed implementation review feedback

* added tests for proposed improvements
  • Loading branch information
a-ignatov-parc committed Dec 19, 2023
1 parent dc047b4 commit aacea34
Show file tree
Hide file tree
Showing 10 changed files with 625 additions and 265 deletions.
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 = {
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 @@ -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

0 comments on commit aacea34

Please sign in to comment.