-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move toJSON method to operationMapper.ts
- Loading branch information
Showing
31 changed files
with
403 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import {JsonLazyRef} from "../domain/JsonLazyRef"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
import {mapGenericsOptions} from "../utils/generics"; | ||
import {createRef, toRef} from "../utils/ref"; | ||
|
||
export function lazyRefMapper(input: JsonLazyRef, options: JsonSchemaOptions) { | ||
const name = input.name; | ||
export function lazyRefMapper(jsonLazyRef: JsonLazyRef, options: JsonSchemaOptions) { | ||
const name = jsonLazyRef.name; | ||
|
||
if (options.$refs?.find((t: any) => t === input.target)) { | ||
return createRef(name, input.schema, options); | ||
if (options.$refs?.find((t: any) => t === jsonLazyRef.target)) { | ||
return createRef(name, jsonLazyRef.schema, options); | ||
} | ||
|
||
options.$refs = [...(options.$refs || []), input.target]; | ||
options.$refs = [...(options.$refs || []), jsonLazyRef.target]; | ||
|
||
const schema = input.toJSON(mapGenericsOptions(options)); | ||
const schema = jsonLazyRef.getType() && execMapper("schema", jsonLazyRef.schema, mapGenericsOptions(options)); | ||
|
||
return toRef(input.schema, schema, options); | ||
return toRef(jsonLazyRef.schema, schema, options); | ||
} | ||
|
||
registerJsonSchemaMapper("lazyRef", lazyRefMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/specs/schema/src/components/operationInParametersMapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {JsonParameter} from "../domain/JsonParameter"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
export function operationInParametersMapper(parameters: JsonParameter[], options: JsonSchemaOptions) { | ||
return parameters.flatMap((parameter) => execMapper("operationInParameter", parameter, options)).filter(Boolean); | ||
} | ||
|
||
registerJsonSchemaMapper("operationInParameters", operationInParametersMapper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {getStatusMessage} from "../constants/httpStatusMessages"; | ||
import {JsonOperation} from "../domain/JsonOperation"; | ||
import {JsonParameter} from "../domain/JsonParameter"; | ||
import {isParameterType, JsonParameterTypes} from "../domain/JsonParameterTypes"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
function extractParameters(jsonOperation: JsonOperation, options: JsonSchemaOptions) { | ||
return jsonOperation | ||
.get("parameters") | ||
.filter((parameter: JsonParameter) => isParameterType(parameter.get("in"))) | ||
.reduce( | ||
(inputs: [any[], JsonParameter[]], parameter: JsonParameter) => { | ||
const [parameters, bodyParameters] = inputs; | ||
|
||
if ([JsonParameterTypes.BODY, JsonParameterTypes.FILES].includes(parameter.get("in"))) { | ||
return [parameters, [...bodyParameters, parameter]]; | ||
} | ||
|
||
return [[...parameters, parameter], bodyParameters]; | ||
}, | ||
[[], []] | ||
); | ||
} | ||
|
||
export function operationMapper(jsonOperation: JsonOperation, {tags = [], defaultTags = [], ...options}: JsonSchemaOptions = {}) { | ||
const {consumes, produces, ...operation} = execMapper("map", jsonOperation, {...options, ignore: ["parameters"]}); | ||
|
||
if (operation.security) { | ||
operation.security = [].concat(operation.security); | ||
} | ||
|
||
if (jsonOperation.get("responses").size === 0) { | ||
operation.responses = { | ||
"200": { | ||
description: getStatusMessage(200) | ||
} | ||
}; | ||
} | ||
|
||
const parametersOptions = { | ||
...options, | ||
consumes: jsonOperation.get("consumes")?.length ? jsonOperation.get("consumes") : ["application/json"] | ||
}; | ||
|
||
const [parameters, bodyParameters] = extractParameters(jsonOperation, parametersOptions); | ||
|
||
operation.parameters = execMapper("operationInParameters", parameters, options); | ||
|
||
if (bodyParameters.length) { | ||
operation.requestBody = execMapper("operationRequestBody", bodyParameters, parametersOptions); | ||
} | ||
|
||
const operationTags = operation.tags?.length ? operation.tags : defaultTags; | ||
|
||
if (operationTags.length) { | ||
operation.tags = operationTags.map(({name}: any) => name); | ||
} | ||
|
||
tags.push(...operationTags); | ||
|
||
return operation; | ||
} | ||
|
||
registerJsonSchemaMapper("operation", operationMapper); |
11 changes: 11 additions & 0 deletions
11
packages/specs/schema/src/components/operationMediaMapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {JsonMedia} from "../domain/JsonMedia"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
export function operationMediaMapper(jsonMedia: JsonMedia, options: JsonSchemaOptions) { | ||
let groups = [...(jsonMedia.groups || [])]; | ||
|
||
return execMapper("map", jsonMedia, {...options, groups, groupsName: jsonMedia.groupsName}); | ||
} | ||
|
||
registerJsonSchemaMapper("operationMedia", operationMediaMapper); |
73 changes: 73 additions & 0 deletions
73
packages/specs/schema/src/components/operationRequestBodyMapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {JsonOperation} from "../domain/JsonOperation"; | ||
import {JsonParameter} from "../domain/JsonParameter"; | ||
import {isParameterType, JsonParameterTypes} from "../domain/JsonParameterTypes"; | ||
import {JsonRequestBody} from "../domain/JsonRequestBody"; | ||
import {JsonSchema} from "../domain/JsonSchema"; | ||
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions"; | ||
import {execMapper, registerJsonSchemaMapper} from "../registries/JsonSchemaMapperContainer"; | ||
|
||
function buildSchemaFromBodyParameters(parameters: JsonParameter[], options: JsonSchemaOptions) { | ||
let schema = new JsonSchema(); | ||
const props: any = {}; | ||
const refs: JsonSchema[] = []; | ||
let propsLength = 0; | ||
|
||
parameters.forEach((parameter) => { | ||
const name = parameter.getName(); | ||
|
||
[...parameter.entries()] | ||
.filter(([key]) => !["in", "name"].includes(key)) | ||
.forEach(([key, value]) => { | ||
if (props[key] === undefined) { | ||
props[key] = value; | ||
} | ||
}); | ||
|
||
const jsonParameter = execMapper("operationInParameter", parameter, options); | ||
|
||
if (name) { | ||
schema.addProperty( | ||
name, | ||
jsonParameter.schema || { | ||
type: jsonParameter.type | ||
} | ||
); | ||
|
||
if (parameter.get("required")) { | ||
schema.addRequired(name); | ||
} | ||
|
||
propsLength++; | ||
} else { | ||
refs.push(jsonParameter); | ||
} | ||
}); | ||
|
||
if (!propsLength) { | ||
if (refs.length === 1) { | ||
return refs[0]; | ||
} | ||
} | ||
|
||
schema.type("object"); | ||
|
||
return { | ||
schema: schema.toJSON(options), | ||
required: false, | ||
...props | ||
}; | ||
} | ||
|
||
export function operationRequestBodyMapper(bodyParameters: JsonParameter[], {consumes, ...options}: JsonSchemaOptions) { | ||
const {schema, examples, in: _, ...props} = buildSchemaFromBodyParameters(bodyParameters, options); | ||
|
||
const requestBody = new JsonRequestBody(props); | ||
|
||
consumes.forEach((consume: string) => { | ||
requestBody.addContent(consume, schema, examples); | ||
}); | ||
|
||
return execMapper("map", requestBody, options); | ||
} | ||
|
||
registerJsonSchemaMapper("operationRequestBody", operationRequestBodyMapper); |
Oops, something went wrong.