refactor: comments address
This commit is contained in:
@@ -8,16 +8,16 @@ type PrimitiveSchemaType = "string" | "integer" | "number" | "boolean"
|
|||||||
|
|
||||||
type SchemaType = "array" | "object" | PrimitiveSchemaType
|
type SchemaType = "array" | "object" | PrimitiveSchemaType
|
||||||
|
|
||||||
type PrimitiveRequestBodyExampleType = number | string | boolean
|
type PrimitiveRequestBodyExample = number | string | boolean
|
||||||
|
|
||||||
type RequestBodyExampleType =
|
type RequestBodyExample =
|
||||||
| { [name: string]: RequestBodyExampleType }
|
| { [name: string]: RequestBodyExample }
|
||||||
| Array<RequestBodyExampleType>
|
| Array<RequestBodyExample>
|
||||||
| PrimitiveRequestBodyExampleType
|
| PrimitiveRequestBodyExample
|
||||||
|
|
||||||
const getPrimitiveTypePlaceholder = (
|
const getPrimitiveTypePlaceholder = (
|
||||||
schemaType: PrimitiveSchemaType
|
schemaType: PrimitiveSchemaType
|
||||||
): PrimitiveRequestBodyExampleType => {
|
): PrimitiveRequestBodyExample => {
|
||||||
switch (schemaType) {
|
switch (schemaType) {
|
||||||
case "string":
|
case "string":
|
||||||
return "string"
|
return "string"
|
||||||
@@ -54,11 +54,11 @@ const isSchemaTypeObject = (schemaType: string): schemaType is "object" =>
|
|||||||
|
|
||||||
const getSampleEnumValueOrPlaceholder = (
|
const getSampleEnumValueOrPlaceholder = (
|
||||||
schema: OpenAPIV2.SchemaObject
|
schema: OpenAPIV2.SchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
pipe(
|
pipe(
|
||||||
schema.enum,
|
schema.enum,
|
||||||
O.fromNullable,
|
O.fromNullable,
|
||||||
O.map((enums) => enums[0] as RequestBodyExampleType),
|
O.map((enums) => enums[0] as RequestBodyExample),
|
||||||
O.altW(() =>
|
O.altW(() =>
|
||||||
pipe(
|
pipe(
|
||||||
schema,
|
schema,
|
||||||
@@ -72,7 +72,7 @@ const getSampleEnumValueOrPlaceholder = (
|
|||||||
|
|
||||||
const generateExampleArrayFromOpenAPIV2ItemsObject = (
|
const generateExampleArrayFromOpenAPIV2ItemsObject = (
|
||||||
items: OpenAPIV2.ItemsObject
|
items: OpenAPIV2.ItemsObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
// ItemsObject can not hold type "object"
|
// ItemsObject can not hold type "object"
|
||||||
// https://swagger.io/specification/v2/#itemsObject
|
// https://swagger.io/specification/v2/#itemsObject
|
||||||
|
|
||||||
@@ -85,25 +85,26 @@ const generateExampleArrayFromOpenAPIV2ItemsObject = (
|
|||||||
flow((items) => items.type as SchemaType, isSchemaTypePrimitive)
|
flow((items) => items.type as SchemaType, isSchemaTypePrimitive)
|
||||||
),
|
),
|
||||||
O.map(
|
O.map(
|
||||||
flow(getSampleEnumValueOrPlaceholder, (arrayItem) =>
|
flow(getSampleEnumValueOrPlaceholder, (arrayItem) => [
|
||||||
Array.of(arrayItem, arrayItem)
|
arrayItem,
|
||||||
)
|
arrayItem,
|
||||||
|
])
|
||||||
),
|
),
|
||||||
O.getOrElse(() =>
|
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
|
||||||
Array.of(
|
[
|
||||||
generateExampleArrayFromOpenAPIV2ItemsObject(
|
generateExampleArrayFromOpenAPIV2ItemsObject(
|
||||||
items.items as OpenAPIV2.ItemsObject
|
items.items as OpenAPIV2.ItemsObject
|
||||||
)
|
),
|
||||||
)
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
const generateRequestBodyExampleFromOpenAPIV2BodySchema = (
|
const generateRequestBodyExampleFromOpenAPIV2BodySchema = (
|
||||||
schema: OpenAPIV2.SchemaObject
|
schema: OpenAPIV2.SchemaObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExample => {
|
||||||
if (schema.example) return schema.example as RequestBodyExampleType
|
if (schema.example) return schema.example as RequestBodyExample
|
||||||
|
|
||||||
const primitiveTypeExample = pipe(
|
const primitiveTypeExample = pipe(
|
||||||
schema,
|
schema,
|
||||||
@@ -155,7 +156,7 @@ const generateRequestBodyExampleFromOpenAPIV2BodySchema = (
|
|||||||
),
|
),
|
||||||
O.getOrElseW(() => [] as [string, OpenAPIV2.SchemaObject][]),
|
O.getOrElseW(() => [] as [string, OpenAPIV2.SchemaObject][]),
|
||||||
A.reduce(
|
A.reduce(
|
||||||
{} as { [name: string]: RequestBodyExampleType },
|
{} as { [name: string]: RequestBodyExample },
|
||||||
(aggregatedExample, property) => {
|
(aggregatedExample, property) => {
|
||||||
const example = generateRequestBodyExampleFromOpenAPIV2BodySchema(
|
const example = generateRequestBodyExampleFromOpenAPIV2BodySchema(
|
||||||
property[1]
|
property[1]
|
||||||
@@ -179,10 +180,6 @@ export const generateRequestBodyExampleFromOpenAPIV2Body = (
|
|||||||
generateRequestBodyExampleFromOpenAPIV2BodySchema
|
generateRequestBodyExampleFromOpenAPIV2BodySchema
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
O.getOrElse(() => "" as RequestBodyExampleType),
|
O.chain(prettyPrintJSON),
|
||||||
(requestBodyExample) =>
|
O.getOrElse(() => "")
|
||||||
pipe(
|
|
||||||
prettyPrintJSON(requestBodyExample),
|
|
||||||
O.getOrElse(() => "")
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { OpenAPIV3 } from "openapi-types"
|
import { OpenAPIV3 } from "openapi-types"
|
||||||
import { pipe } from "fp-ts/function"
|
import { pipe } from "fp-ts/function"
|
||||||
import * as A from "fp-ts/Array"
|
|
||||||
import * as O from "fp-ts/Option"
|
import * as O from "fp-ts/Option"
|
||||||
|
import { tupleToRecord } from "~/helpers/functional/record"
|
||||||
|
|
||||||
type SchemaType =
|
type SchemaType =
|
||||||
| OpenAPIV3.ArraySchemaObjectType
|
| OpenAPIV3.ArraySchemaObjectType
|
||||||
@@ -9,12 +9,12 @@ type SchemaType =
|
|||||||
|
|
||||||
type PrimitiveSchemaType = Exclude<SchemaType, "array" | "object">
|
type PrimitiveSchemaType = Exclude<SchemaType, "array" | "object">
|
||||||
|
|
||||||
type PrimitiveRequestBodyExampleType = string | number | boolean | null
|
type PrimitiveRequestBodyExample = string | number | boolean | null
|
||||||
|
|
||||||
type RequestBodyExampleType =
|
type RequestBodyExample =
|
||||||
| PrimitiveRequestBodyExampleType
|
| PrimitiveRequestBodyExample
|
||||||
| Array<RequestBodyExampleType>
|
| Array<RequestBodyExample>
|
||||||
| { [name: string]: RequestBodyExampleType }
|
| { [name: string]: RequestBodyExample }
|
||||||
|
|
||||||
const isSchemaTypePrimitive = (
|
const isSchemaTypePrimitive = (
|
||||||
schemaType: SchemaType
|
schemaType: SchemaType
|
||||||
@@ -23,7 +23,7 @@ const isSchemaTypePrimitive = (
|
|||||||
|
|
||||||
const getPrimitiveTypePlaceholder = (
|
const getPrimitiveTypePlaceholder = (
|
||||||
primitiveType: PrimitiveSchemaType
|
primitiveType: PrimitiveSchemaType
|
||||||
): PrimitiveRequestBodyExampleType => {
|
): PrimitiveRequestBodyExample => {
|
||||||
switch (primitiveType) {
|
switch (primitiveType) {
|
||||||
case "number":
|
case "number":
|
||||||
return 0.0
|
return 0.0
|
||||||
@@ -40,46 +40,34 @@ const getPrimitiveTypePlaceholder = (
|
|||||||
// TODO(agarwal): Use Enum values, if any
|
// TODO(agarwal): Use Enum values, if any
|
||||||
const generatePrimitiveRequestBodyExample = (
|
const generatePrimitiveRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV3.NonArraySchemaObject
|
schemaObject: OpenAPIV3.NonArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
getPrimitiveTypePlaceholder(schemaObject.type as PrimitiveSchemaType)
|
getPrimitiveTypePlaceholder(schemaObject.type as PrimitiveSchemaType)
|
||||||
|
|
||||||
// Use carefully, call only when type is object
|
// Use carefully, call only when type is object
|
||||||
const generateObjectRequestBodyExample = (
|
const generateObjectRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV3.NonArraySchemaObject
|
schemaObject: OpenAPIV3.NonArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
pipe(
|
pipe(
|
||||||
schemaObject.properties,
|
schemaObject.properties,
|
||||||
O.fromNullable,
|
O.fromNullable,
|
||||||
O.map(
|
O.map(Object.entries),
|
||||||
(properties) =>
|
|
||||||
Object.entries(properties) as [string, OpenAPIV3.SchemaObject][]
|
|
||||||
),
|
|
||||||
O.getOrElseW(() => [] as [string, OpenAPIV3.SchemaObject][]),
|
O.getOrElseW(() => [] as [string, OpenAPIV3.SchemaObject][]),
|
||||||
A.reduce(
|
tupleToRecord
|
||||||
{} as { [name: string]: RequestBodyExampleType },
|
|
||||||
(aggregatedExample, property) => {
|
|
||||||
aggregatedExample[property[0]] =
|
|
||||||
generateRequestBodyExampleFromSchemaObject(property[1])
|
|
||||||
return aggregatedExample
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const generateArrayRequestBodyExample = (
|
const generateArrayRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV3.ArraySchemaObject
|
schemaObject: OpenAPIV3.ArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample => [
|
||||||
Array.of(
|
generateRequestBodyExampleFromSchemaObject(
|
||||||
generateRequestBodyExampleFromSchemaObject(
|
schemaObject.items as OpenAPIV3.SchemaObject
|
||||||
schemaObject.items as OpenAPIV3.SchemaObject
|
),
|
||||||
)
|
]
|
||||||
)
|
|
||||||
|
|
||||||
const generateRequestBodyExampleFromSchemaObject = (
|
const generateRequestBodyExampleFromSchemaObject = (
|
||||||
schemaObject: OpenAPIV3.SchemaObject
|
schemaObject: OpenAPIV3.SchemaObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExample => {
|
||||||
// TODO: Handle schema objects with allof
|
// TODO: Handle schema objects with allof
|
||||||
if (schemaObject.example)
|
if (schemaObject.example) return schemaObject.example as RequestBodyExample
|
||||||
return schemaObject.example as RequestBodyExampleType
|
|
||||||
|
|
||||||
// If request body can be oneof or allof several schema, choose the first schema to generate an example
|
// If request body can be oneof or allof several schema, choose the first schema to generate an example
|
||||||
if (schemaObject.oneOf)
|
if (schemaObject.oneOf)
|
||||||
@@ -110,10 +98,9 @@ const generateRequestBodyExampleFromSchemaObject = (
|
|||||||
|
|
||||||
export const generateRequestBodyExampleFromMediaObject = (
|
export const generateRequestBodyExampleFromMediaObject = (
|
||||||
mediaObject: OpenAPIV3.MediaTypeObject
|
mediaObject: OpenAPIV3.MediaTypeObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExample => {
|
||||||
if (mediaObject.example) return mediaObject.example as RequestBodyExampleType
|
if (mediaObject.example) return mediaObject.example as RequestBodyExample
|
||||||
if (mediaObject.examples)
|
if (mediaObject.examples) return mediaObject.examples[0] as RequestBodyExample
|
||||||
return mediaObject.examples[0] as RequestBodyExampleType
|
|
||||||
return mediaObject.schema
|
return mediaObject.schema
|
||||||
? generateRequestBodyExampleFromSchemaObject(
|
? generateRequestBodyExampleFromSchemaObject(
|
||||||
mediaObject.schema as OpenAPIV3.SchemaObject
|
mediaObject.schema as OpenAPIV3.SchemaObject
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ type PrimitiveSchemaType = Exclude<
|
|||||||
"object"
|
"object"
|
||||||
>
|
>
|
||||||
|
|
||||||
type PrimitiveRequestBodyExampleType = string | number | boolean | null
|
type PrimitiveRequestBodyExample = string | number | boolean | null
|
||||||
|
|
||||||
type RequestBodyExampleType =
|
type RequestBodyExample =
|
||||||
| PrimitiveRequestBodyExampleType
|
| PrimitiveRequestBodyExample
|
||||||
| Array<RequestBodyExampleType>
|
| Array<RequestBodyExample>
|
||||||
| { [name: string]: RequestBodyExampleType }
|
| { [name: string]: RequestBodyExample }
|
||||||
|
|
||||||
const isSchemaTypePrimitive = (
|
const isSchemaTypePrimitive = (
|
||||||
schemaType: SchemaType
|
schemaType: SchemaType
|
||||||
@@ -32,7 +32,7 @@ const isSchemaTypePrimitive = (
|
|||||||
|
|
||||||
const getPrimitiveTypePlaceholder = (
|
const getPrimitiveTypePlaceholder = (
|
||||||
primitiveType: PrimitiveSchemaType
|
primitiveType: PrimitiveSchemaType
|
||||||
): PrimitiveRequestBodyExampleType => {
|
): PrimitiveRequestBodyExample => {
|
||||||
switch (primitiveType) {
|
switch (primitiveType) {
|
||||||
case "number":
|
case "number":
|
||||||
return 0.0
|
return 0.0
|
||||||
@@ -50,13 +50,13 @@ const getPrimitiveTypePlaceholder = (
|
|||||||
// TODO(agarwal): Use Enum values, if any
|
// TODO(agarwal): Use Enum values, if any
|
||||||
const generatePrimitiveRequestBodyExample = (
|
const generatePrimitiveRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV31.NonArraySchemaObject
|
schemaObject: OpenAPIV31.NonArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
getPrimitiveTypePlaceholder(schemaObject.type as PrimitiveSchemaType)
|
getPrimitiveTypePlaceholder(schemaObject.type as PrimitiveSchemaType)
|
||||||
|
|
||||||
// Use carefully, the schema type should necessarily be object
|
// Use carefully, the schema type should necessarily be object
|
||||||
const generateObjectRequestBodyExample = (
|
const generateObjectRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV31.NonArraySchemaObject
|
schemaObject: OpenAPIV31.NonArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
pipe(
|
pipe(
|
||||||
schemaObject.properties,
|
schemaObject.properties,
|
||||||
O.fromNullable,
|
O.fromNullable,
|
||||||
@@ -66,7 +66,7 @@ const generateObjectRequestBodyExample = (
|
|||||||
),
|
),
|
||||||
O.getOrElseW(() => [] as [string, OpenAPIV31.SchemaObject][]),
|
O.getOrElseW(() => [] as [string, OpenAPIV31.SchemaObject][]),
|
||||||
A.reduce(
|
A.reduce(
|
||||||
{} as { [name: string]: RequestBodyExampleType },
|
{} as { [name: string]: RequestBodyExample },
|
||||||
(aggregatedExample, property) => {
|
(aggregatedExample, property) => {
|
||||||
aggregatedExample[property[0]] =
|
aggregatedExample[property[0]] =
|
||||||
generateRequestBodyExampleFromSchemaObject(property[1])
|
generateRequestBodyExampleFromSchemaObject(property[1])
|
||||||
@@ -78,39 +78,34 @@ const generateObjectRequestBodyExample = (
|
|||||||
// Use carefully, the schema type should necessarily be mixed array
|
// Use carefully, the schema type should necessarily be mixed array
|
||||||
const generateMixedArrayRequestBodyEcample = (
|
const generateMixedArrayRequestBodyEcample = (
|
||||||
schemaObject: OpenAPIV31.SchemaObject
|
schemaObject: OpenAPIV31.SchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample =>
|
||||||
pipe(
|
pipe(
|
||||||
schemaObject,
|
schemaObject,
|
||||||
(schemaObject) => schemaObject.type as MixedArraySchemaType,
|
(schemaObject) => schemaObject.type as MixedArraySchemaType,
|
||||||
A.reduce(
|
A.reduce([] as Array<RequestBodyExample>, (aggregatedExample, itemType) => {
|
||||||
[] as Array<RequestBodyExampleType>,
|
// TODO: Figure out how to include non-primitive types as well
|
||||||
(aggregatedExample, itemType) => {
|
if (isSchemaTypePrimitive(itemType)) {
|
||||||
// TODO: Figure out how to include non-primitive types as well
|
aggregatedExample.push(getPrimitiveTypePlaceholder(itemType))
|
||||||
if (isSchemaTypePrimitive(itemType)) {
|
|
||||||
aggregatedExample.push(getPrimitiveTypePlaceholder(itemType))
|
|
||||||
}
|
|
||||||
return aggregatedExample
|
|
||||||
}
|
}
|
||||||
)
|
return aggregatedExample
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
const generateArrayRequestBodyExample = (
|
const generateArrayRequestBodyExample = (
|
||||||
schemaObject: OpenAPIV31.ArraySchemaObject
|
schemaObject: OpenAPIV31.ArraySchemaObject
|
||||||
): RequestBodyExampleType =>
|
): RequestBodyExample => [
|
||||||
Array.of(
|
generateRequestBodyExampleFromSchemaObject(
|
||||||
generateRequestBodyExampleFromSchemaObject(
|
schemaObject.items as OpenAPIV31.SchemaObject
|
||||||
schemaObject.items as OpenAPIV31.SchemaObject
|
),
|
||||||
)
|
]
|
||||||
)
|
|
||||||
|
|
||||||
const generateRequestBodyExampleFromSchemaObject = (
|
const generateRequestBodyExampleFromSchemaObject = (
|
||||||
schemaObject: OpenAPIV31.SchemaObject
|
schemaObject: OpenAPIV31.SchemaObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExample => {
|
||||||
// TODO: Handle schema objects with oneof or anyof
|
// TODO: Handle schema objects with oneof or anyof
|
||||||
if (schemaObject.example)
|
if (schemaObject.example) return schemaObject.example as RequestBodyExample
|
||||||
return schemaObject.example as RequestBodyExampleType
|
|
||||||
if (schemaObject.examples)
|
if (schemaObject.examples)
|
||||||
return schemaObject.examples[0] as RequestBodyExampleType
|
return schemaObject.examples[0] as RequestBodyExample
|
||||||
if (!schemaObject.type) return ""
|
if (!schemaObject.type) return ""
|
||||||
if (isSchemaTypePrimitive(schemaObject.type))
|
if (isSchemaTypePrimitive(schemaObject.type))
|
||||||
return generatePrimitiveRequestBodyExample(
|
return generatePrimitiveRequestBodyExample(
|
||||||
@@ -125,10 +120,9 @@ const generateRequestBodyExampleFromSchemaObject = (
|
|||||||
|
|
||||||
export const generateRequestBodyExampleFromMediaObject = (
|
export const generateRequestBodyExampleFromMediaObject = (
|
||||||
mediaObject: OpenAPIV31.MediaTypeObject
|
mediaObject: OpenAPIV31.MediaTypeObject
|
||||||
): RequestBodyExampleType => {
|
): RequestBodyExample => {
|
||||||
if (mediaObject.example) return mediaObject.example as RequestBodyExampleType
|
if (mediaObject.example) return mediaObject.example as RequestBodyExample
|
||||||
if (mediaObject.examples)
|
if (mediaObject.examples) return mediaObject.examples[0] as RequestBodyExample
|
||||||
return mediaObject.examples[0] as RequestBodyExampleType
|
|
||||||
return mediaObject.schema
|
return mediaObject.schema
|
||||||
? generateRequestBodyExampleFromSchemaObject(mediaObject.schema)
|
? generateRequestBodyExampleFromSchemaObject(mediaObject.schema)
|
||||||
: ""
|
: ""
|
||||||
|
|||||||
Reference in New Issue
Block a user