Skip to content

Commit

Permalink
fix deeply nested values from having empty properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Oct 27, 2023
1 parent 4142ca6 commit 18ed010
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deeply nested objects with file 1`] = `
"from pprint import pprint
from groundx import Groundx
groundx = Groundx(
api_key="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
response = groundx.documents.upload_local([{
"blob": open("file.txt", "rb"),
"metadata": {
"bucket_id": 321,
"file_name": "321",
"file_type": "docx"
}
}])
pprint(response.body)"
`;

exports[`example with boolean 1`] = `
"from pprint import pprint
from snaptrade_client import SnapTrade
Expand Down Expand Up @@ -101,7 +120,6 @@ response = groundx.documents.upload_local([{
"bucket_id": 321,
"file_name": "321",
"file_type": "txt",
"metadata": "",
"callback_data": "321",
"callback_url": "3213"
}
Expand All @@ -110,10 +128,7 @@ response = groundx.documents.upload_local([{
"metadata": {
"bucket_id": 321,
"file_name": "321",
"file_type": "txt",
"metadata": "",
"callback_data": "",
"callback_url": ""
"file_type": "txt"
}
}])
pprint(response.body)"
Expand Down
111 changes: 111 additions & 0 deletions generator/konfig-next-app/src/utils/code-generator-python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,117 @@ import { HttpMethodsEnum } from 'konfig-lib'
import { CodeGeneratorConstructorArgs } from './code-generator'
import { CodeGeneratorPython } from './code-generator-python'

test('deeply nested objects with file', async () => {
const args: CodeGeneratorConstructorArgs = {
httpMethod: HttpMethodsEnum.POST,
path: '/v1/ingest/documents/local',
parameters: [],
requestBody: {
name: '',
in: 'body',
schema: {
type: 'array',
items: {
type: 'object',
required: ['blob', 'metadata'],
properties: {
blob: {
description: 'The actual file being uploaded.',
type: 'string',
format: 'binary',
},
metadata: {
type: 'object',
required: ['bucketId', 'fileName', 'fileType'],
properties: {
bucketId: {
type: 'integer',
example: 1234,
},
fileName: {
type: 'string',
example: 'my_file.txt',
},
fileType: {
type: 'string',
enum: ['txt', 'docx', 'pptx', 'xlsx', 'pdf', 'png', 'jpg'],
},
metadata: {
type: 'object',
example: {
key: 'value',
},
},
callbackData: {
type: 'string',
example: 'my_callback_data',
},
callbackUrl: {
type: 'string',
example: 'https://my.callback.url.com',
},
},
},
},
},
},
isRequestBody: true,
},
securitySchemes: {
ApiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-API-Key',
},
},
formData: {
parameters: {
documentId: '',
},
security: {
ApiKeyAuth: {
type: 'apiKey',
in: 'header',
key: 'X-API-Key',
value: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
},
},
requestBody: [
{
blob: new File([], 'file.txt'),
metadata: {
bucketId: 321,
fileName: '321',
fileType: 'docx',
metadata: '',
callbackData: '',
callbackUrl: '',
},
},
],
},
languageConfigurations: {
typescript: {
clientName: 'Groundx',
packageName: 'groundx-typescript-sdk',
},
python: {
clientName: 'Groundx',
packageName: 'groundx',
},
},
servers: ['https://api.groundx.ai/api'],
operationId: 'Document_uploadLocal',
tag: 'Documents',
basePath: 'https://api.groundx.ai/api',
oauthTokenUrl: null,
originalOauthTokenUrl: null,
requestBodyRequired: false,
}
const code = await new CodeGeneratorPython(args).snippet()
expect(code).toMatchSnapshot()
})

test('nested objects does not have empty properties', async () => {
const args: CodeGeneratorConstructorArgs = {
httpMethod: HttpMethodsEnum.POST,
Expand Down
4 changes: 3 additions & 1 deletion generator/konfig-next-app/src/utils/code-generator-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ from ${this.packageName} import ${this.clientName}`

get args(): string {
if (this.isArrayRequestBody) {
const arrayValue = this.requestBodyValue
const arrayValue = this.recursivelyRemoveEmptyValues(
this.requestBodyValue
)
if (Array.isArray(arrayValue)) {
return `[${arrayValue
.map((v) => this.toPythonLiteralString(v))
Expand Down
2 changes: 2 additions & 0 deletions generator/konfig-next-app/src/utils/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export abstract class CodeGenerator {
return filtered
}

if (object instanceof File) return object

const clone = { ...object }
Object.entries(object).forEach(([key, value]) => {
if (typeof value === 'object') {
Expand Down

0 comments on commit 18ed010

Please sign in to comment.