From 3a41d79c2f9c286dffb1df4a00d587974bae1c46 Mon Sep 17 00:00:00 2001 From: Rishabh Agarwal Date: Wed, 27 Apr 2022 23:48:26 +0530 Subject: [PATCH] feat: handle oneof and anyof --- .../import-export/import/openapi/exampleV3.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV3.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV3.ts index 19432ec3f..59571dfbc 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV3.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV3.ts @@ -77,18 +77,32 @@ const generateArrayRequestBodyExample = ( const generateRequestBodyExampleFromSchemaObject = ( schemaObject: OpenAPIV3.SchemaObject ): RequestBodyExampleType => { - // TODO: Handle schema objects with oneof or anyof + // TODO: Handle schema objects with allof if (schemaObject.example) return schemaObject.example as RequestBodyExampleType + + // If request body can be oneof or allof several schema, choose the first schema to generate an example + if (schemaObject.oneOf) + return generateRequestBodyExampleFromSchemaObject( + schemaObject.oneOf[0] as OpenAPIV3.SchemaObject + ) + if (schemaObject.anyOf) + return generateRequestBodyExampleFromSchemaObject( + schemaObject.anyOf[0] as OpenAPIV3.SchemaObject + ) + if (!schemaObject.type) return "" + if (isSchemaTypePrimitive(schemaObject.type)) return generatePrimitiveRequestBodyExample( schemaObject as OpenAPIV3.NonArraySchemaObject ) + if (schemaObject.type === "object") return generateObjectRequestBodyExample( schemaObject as OpenAPIV3.NonArraySchemaObject ) + return generateArrayRequestBodyExample( schemaObject as OpenAPIV3.ArraySchemaObject )