refactor: address comments
This commit is contained in:
committed by
Andrew Bastin
parent
ed49c0b72c
commit
3afd2c1cf2
@@ -7,3 +7,6 @@ import * as O from "fp-ts/Option"
|
|||||||
*/
|
*/
|
||||||
export const safeParseJSON = (str: string): O.Option<object> =>
|
export const safeParseJSON = (str: string): O.Option<object> =>
|
||||||
O.tryCatch(() => JSON.parse(str))
|
O.tryCatch(() => JSON.parse(str))
|
||||||
|
|
||||||
|
export const prettyPrintStringifyJSON = (jsonObject: any): O.Option<string> =>
|
||||||
|
O.tryCatch(() => JSON.stringify(jsonObject, null, "\t"))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { OpenAPIV2 } from "openapi-types"
|
|||||||
import * as O from "fp-ts/Option"
|
import * as O from "fp-ts/Option"
|
||||||
import { pipe, flow } from "fp-ts/function"
|
import { pipe, flow } from "fp-ts/function"
|
||||||
import * as A from "fp-ts/Array"
|
import * as A from "fp-ts/Array"
|
||||||
|
import { prettyPrintStringifyJSON } from "~/helpers/functional/json"
|
||||||
|
|
||||||
type PrimitiveSchemaType = "string" | "integer" | "number" | "boolean"
|
type PrimitiveSchemaType = "string" | "integer" | "number" | "boolean"
|
||||||
|
|
||||||
@@ -53,52 +54,51 @@ const isSchemaTypeObject = (schemaType: string): schemaType is "object" =>
|
|||||||
|
|
||||||
const getSampleEnumValueOrPlaceholder = (
|
const getSampleEnumValueOrPlaceholder = (
|
||||||
schema: OpenAPIV2.SchemaObject
|
schema: OpenAPIV2.SchemaObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExampleType =>
|
||||||
const enumValue = pipe(
|
pipe(
|
||||||
schema.enum,
|
schema.enum,
|
||||||
O.fromNullable,
|
O.fromNullable,
|
||||||
O.map((enums) => enums[0] as RequestBodyExampleType)
|
O.map((enums) => enums[0] as RequestBodyExampleType),
|
||||||
)
|
O.altW(() =>
|
||||||
|
pipe(
|
||||||
if (O.isSome(enumValue)) return enumValue.value
|
schema,
|
||||||
|
getSchemaTypeFromSchemaObject,
|
||||||
return pipe(
|
O.filter(isSchemaTypePrimitive),
|
||||||
schema,
|
O.map(getPrimitiveTypePlaceholder)
|
||||||
getSchemaTypeFromSchemaObject,
|
)
|
||||||
O.filter(isSchemaTypePrimitive),
|
),
|
||||||
O.map(getPrimitiveTypePlaceholder),
|
|
||||||
O.getOrElseW(() => "")
|
O.getOrElseW(() => "")
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const generateExampleArrayFromOpenAPIV2ItemsObject = (
|
const generateExampleArrayFromOpenAPIV2ItemsObject = (
|
||||||
items: OpenAPIV2.ItemsObject
|
items: OpenAPIV2.ItemsObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExampleType =>
|
||||||
// ItemsObject can not hold type "object"
|
// ItemsObject can not hold type "object"
|
||||||
// https://swagger.io/specification/v2/#itemsObject
|
// https://swagger.io/specification/v2/#itemsObject
|
||||||
|
|
||||||
// TODO : Handle array of objects
|
// TODO : Handle array of objects
|
||||||
// https://stackoverflow.com/questions/60490974/how-to-define-an-array-of-objects-in-openapi-2-0
|
// https://stackoverflow.com/questions/60490974/how-to-define-an-array-of-objects-in-openapi-2-0
|
||||||
|
|
||||||
const primitivePlaceholder = pipe(
|
pipe(
|
||||||
items,
|
items,
|
||||||
O.fromPredicate(
|
O.fromPredicate(
|
||||||
flow((items) => items.type as SchemaType, isSchemaTypePrimitive)
|
flow((items) => items.type as SchemaType, isSchemaTypePrimitive)
|
||||||
),
|
),
|
||||||
O.map(getSampleEnumValueOrPlaceholder)
|
O.map(
|
||||||
)
|
flow(getSampleEnumValueOrPlaceholder, (arrayItem) =>
|
||||||
|
Array.of(arrayItem, arrayItem)
|
||||||
if (O.isSome(primitivePlaceholder))
|
)
|
||||||
return Array.of(primitivePlaceholder.value, primitivePlaceholder.value)
|
),
|
||||||
|
O.getOrElse(() =>
|
||||||
// If the type is not primitive, it is "array"
|
// If the type is not primitive, it is "array"
|
||||||
// items property is required if type is array
|
// items property is required if type is array
|
||||||
return Array.of(
|
Array.of(
|
||||||
generateExampleArrayFromOpenAPIV2ItemsObject(
|
generateExampleArrayFromOpenAPIV2ItemsObject(
|
||||||
items.items as OpenAPIV2.ItemsObject
|
items.items as OpenAPIV2.ItemsObject
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const generateRequestBodyExampleFromOpenAPIV2BodySchema = (
|
const generateRequestBodyExampleFromOpenAPIV2BodySchema = (
|
||||||
schema: OpenAPIV2.SchemaObject
|
schema: OpenAPIV2.SchemaObject
|
||||||
@@ -180,5 +180,9 @@ export const generateRequestBodyExampleFromOpenAPIV2Body = (
|
|||||||
)
|
)
|
||||||
),
|
),
|
||||||
O.getOrElse(() => "" as RequestBodyExampleType),
|
O.getOrElse(() => "" as RequestBodyExampleType),
|
||||||
(requestBodyExample) => JSON.stringify(requestBodyExample, null, "\t") // Using a tab character mimics standard pretty-print appearance
|
(requestBodyExample) =>
|
||||||
|
pipe(
|
||||||
|
prettyPrintStringifyJSON(requestBodyExample),
|
||||||
|
O.getOrElse(() => "")
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "../"
|
|||||||
import { generateRequestBodyExampleFromMediaObject as generateExampleV31 } from "./exampleV31"
|
import { generateRequestBodyExampleFromMediaObject as generateExampleV31 } from "./exampleV31"
|
||||||
import { generateRequestBodyExampleFromMediaObject as generateExampleV3 } from "./exampleV3"
|
import { generateRequestBodyExampleFromMediaObject as generateExampleV3 } from "./exampleV3"
|
||||||
import { generateRequestBodyExampleFromOpenAPIV2Body } from "./exampleV2"
|
import { generateRequestBodyExampleFromOpenAPIV2Body } from "./exampleV2"
|
||||||
|
import { prettyPrintStringifyJSON } from "~/helpers/functional/json"
|
||||||
|
|
||||||
export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
|
export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
|
||||||
|
|
||||||
@@ -205,12 +206,13 @@ const parseOpenAPIV3Body = (
|
|||||||
OpenAPIV3.MediaTypeObject | OpenAPIV31.MediaTypeObject
|
OpenAPIV3.MediaTypeObject | OpenAPIV31.MediaTypeObject
|
||||||
] = objs[0]
|
] = objs[0]
|
||||||
|
|
||||||
const exampleBody = JSON.stringify(
|
const exampleBody = pipe(
|
||||||
isV31Request
|
prettyPrintStringifyJSON(
|
||||||
? generateExampleV31(media as OpenAPIV31.MediaTypeObject)
|
isV31Request
|
||||||
: generateExampleV3(media as OpenAPIV3.MediaTypeObject),
|
? generateExampleV31(media as OpenAPIV31.MediaTypeObject)
|
||||||
null,
|
: generateExampleV3(media as OpenAPIV3.MediaTypeObject)
|
||||||
"\t"
|
),
|
||||||
|
O.getOrElse(() => "")
|
||||||
)
|
)
|
||||||
|
|
||||||
return contentType in knownContentTypes
|
return contentType in knownContentTypes
|
||||||
|
|||||||
Reference in New Issue
Block a user