Skip to content

Commit

Permalink
[ENG-1256] Regenerate eyelevel’s SDKs (#265)
Browse files Browse the repository at this point in the history
* support array type for singleParameterExample

* support array request body in typescript generated example

* fix readmeHeader for Python SDK

* fix readme image in Pypi
  • Loading branch information
dphuang2 authored Oct 9, 2023
1 parent db0eb6e commit 387be7b
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 82 deletions.
72 changes: 60 additions & 12 deletions generator/konfig-dash/api/src/functions/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {
},
config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git }),
...prepareJavaRequestProperties({
body,
git,
generatorConfig: body.generators.objc,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -288,7 +292,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {
},
config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -324,7 +332,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {
},
config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
readmeSnippet: generatorConfig.readmeSnippet,
readmeSupportingDescriptionSnippet:
Expand Down Expand Up @@ -375,7 +387,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {
},
config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -434,7 +450,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {

config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -473,7 +493,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {

config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -517,7 +541,11 @@ export const myHandler = async (event: APIGatewayEvent, context: Context) => {

config: {
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
useDescriptionInOperationTableDocumentation:
generatorConfig.useDescriptionInOperationTableDocumentation,
omitInfoDescription: body.omitInfoDescription,
Expand Down Expand Up @@ -662,7 +690,11 @@ async function queueGoGeneration({
config: {
outputDirectoryName,
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -714,7 +746,11 @@ async function queuePhpGeneration({
config: {
outputDirectoryName,
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -775,7 +811,11 @@ async function queueDartGeneration({
config: {
outputDirectoryName,
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -844,7 +884,11 @@ async function queueJavaGeneration({
config: {
outputDirectoryName,
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down Expand Up @@ -927,7 +971,11 @@ async function queueTypeScriptGeneration({
config: {
outputDirectoryName, // this is so the directory name from additionalGenerators does not conflict with the one under generators
additionalProperties: {
...prepareJavaRequestProperties({ body, git: generatorConfig.git }),
...prepareJavaRequestProperties({
body,
git: generatorConfig.git,
generatorConfig,
}),
omitInfoDescription: body.omitInfoDescription,
tagPriority: body.tagPriority,
readmeOperation: body.readmeOperation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { GenerateRequestBodyType, GeneratorGitConfig } from 'konfig-lib'
import {
GenerateRequestBodyType,
GeneratorGitConfig,
KonfigYamlAdditionalGeneratorConfig,
KonfigYamlGeneratorConfig,
} from 'konfig-lib'

/**
* Used in preparation to send a request to the Java generator API for all
Expand All @@ -16,9 +21,13 @@ import { GenerateRequestBodyType, GeneratorGitConfig } from 'konfig-lib'
export function prepareJavaRequestProperties({
body,
git,
generatorConfig,
}: {
body: GenerateRequestBodyType
git: GeneratorGitConfig
generatorConfig:
| KonfigYamlGeneratorConfig
| KonfigYamlAdditionalGeneratorConfig
}): Record<string, unknown> {
const properties: Record<string, unknown> = {}

Expand All @@ -29,6 +38,14 @@ export function prepareJavaRequestProperties({
}
}

if ('repoName' in git) {
properties['gitRepoName'] = git.repoName
}

if ('outputDirectory' in generatorConfig) {
properties['outputDirectory'] = generatorConfig.outputDirectory
}

if ('defaultBranch' in git) {
properties['gitDefaultBranch'] = git.defaultBranch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -4089,7 +4090,11 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required,
} else if (ModelUtils.isAnyType(p)) {
updatePropertyForAnyType(property, p);
} else if (ModelUtils.isTypeObjectSchema(pDeref)) {
updatePropertyForObject(property, p);
// I keep going back and forth between p / pDeref for second argument here
// we really need tests so I can make changes to fix customer bugs and be confident I didn't break
// something else. Or else we could be going in circles / making backward progress when fixing bugs for
// customers.
updatePropertyForObject(property, pDeref);
} else if (!ModelUtils.isNullType(p)) {
// referenced model
;
Expand Down Expand Up @@ -4601,6 +4606,7 @@ public CodegenOperation fromOperation(String path,
List<CodegenParameter> optionalNonBodyParams = new ArrayList<>();
List<CodegenParameter> optionalParamsWithRequestBodyProperties = new ArrayList<>();


CodegenParameter bodyParam = null;
RequestBody requestBody = operation.getRequestBody();
if (requestBody != null) {
Expand Down Expand Up @@ -4632,6 +4638,18 @@ public CodegenOperation fromOperation(String path,
flattenedParamsFromRequestBodyProperties.add(cp);
}

requestBody = ModelUtils.getReferencedRequestBody(this.openAPI, requestBody);

// Still add body parameter to parameters
String bodyParameterName = "";
if (op.vendorExtensions != null && op.vendorExtensions.containsKey("x-codegen-request-body-name")) {
bodyParameterName = (String) op.vendorExtensions.get("x-codegen-request-body-name");
}
bodyParam = fromRequestBody(requestBody, imports, bodyParameterName);
bodyParam.description = escapeText(requestBody.getDescription());
postProcessParameter(bodyParam);

bodyParams.add(bodyParam);
} else {
// process body parameter
requestBody = ModelUtils.getReferencedRequestBody(this.openAPI, requestBody);
Expand Down Expand Up @@ -7470,7 +7488,8 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
} else {
final ArraySchema arraySchema = (ArraySchema) schema;
Schema inner = getSchemaItems(arraySchema);
CodegenProperty codegenProperty = fromProperty("property", arraySchema, false);
CodegenProperty codegenProperty = fromProperty("item", arraySchema, false);

if (codegenProperty == null) {
throw new RuntimeException("CodegenProperty cannot be null. arraySchema for debugging: " + arraySchema);
}
Expand Down Expand Up @@ -7506,6 +7525,7 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
} else {
codegenParameter.baseName = bodyParameterName;
}

codegenParameter.paramName = toArrayModelParamName(codegenParameter.baseName);
codegenParameter.items = codegenProperty.items;
codegenParameter.mostInnerItems = codegenProperty.mostInnerItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{{#with readmeHeader}}
<div align="center">

[![Visit {{{title}}}](https://raw.githubusercontent.com/{{gitUserId}}/{{gitRepoName}}/HEAD/{{{@root.outputDirectory}}}/header.png)]({{{url}}})

# {{{title}}}

[![Visit {{{title}}}](./header.png)]({{{url}}})
{{> readme_description}}

{{> readme_badges}}

Expand All @@ -13,15 +15,11 @@
{{else}}
# {{{projectName}}}

{{> readme_description}}

{{> readme_badges}}
{{/if}}

{{#unless omitInfoDescription}}
{{#if appDescriptionWithNewLines}}
{{{appDescriptionWithNewLines}}}

{{/if}}
{{/unless}}
## Requirements

Python {{{generatorLanguageVersion}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1798,8 +1798,8 @@ class RequestBody(StyleFormSerializer, JSONDetector):
def __serialize_multipart_form_data(
self, in_data: Schema
) -> typing.Dict[str, typing.Tuple[RequestField, ...]]:
if not isinstance(in_data, frozendict.frozendict):
raise ValueError(f'Unable to serialize {in_data} to multipart/form-data because it is not a dict of data')
if not isinstance(in_data, frozendict.frozendict) and not isinstance(in_data, list) and not isinstance(in_data, tuple):
raise ValueError(f'Unable to serialize {in_data} to multipart/form-data because it is not a dict of data or a list of data')
"""
In a multipart/form-data request body, each schema property, or each element of a schema array property,
takes a section in the payload with an internal header as defined by RFC7578. The serialization strategy
Expand All @@ -1814,24 +1814,33 @@ class RequestBody(StyleFormSerializer, JSONDetector):
If the property is a type: string with a contentEncoding, the default Content-Type is application/octet-stream
"""
fields: typing.List[RequestField] = []
for key, value in in_data.items():
if isinstance(value, tuple):
if value:
# values use explode = True, so the code makes a RequestField for each item with name=key
for item in value:
request_field = self.__multipart_form_item(key=key, value=item)

def add_field(data):
for key, value in data.items():
if isinstance(value, tuple):
if value:
# values use explode = True, so the code makes a RequestField for each item with name=key
for item in value:
request_field = self.__multipart_form_item(key=key, value=item)
fields.append(request_field)
else:
# send an empty array as json because exploding will not send it
request_field = self.__multipart_json_item(key=key, value=value)
fields.append(request_field)
else:
# send an empty array as json because exploding will not send it
request_field = self.__multipart_json_item(key=key, value=value)
request_field = self.__multipart_form_item(key=key, value=value)
fields.append(request_field)
else:
request_field = self.__multipart_form_item(key=key, value=value)
fields.append(request_field)

if isinstance(in_data, list) or isinstance(in_data, tuple):
for item in in_data:
add_field(item)
else:
add_field(in_data)

# This is necessary to fill the "Content-Disposition" header needed for naming fields in multipart
for field in fields:
field.make_multipart(content_type=field.headers["Content-Type"])

return dict(fields=tuple(fields))

def __serialize_application_octet_stream(self, in_data: BinarySchema) -> typing.Dict[str, bytes]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#unless omitInfoDescription}}
{{#if appDescriptionWithNewLines}}
{{{appDescriptionWithNewLines}}}

{{/if}}
{{/unless}}
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,25 @@ yarn add {{npmName}}
```

{{#hasParams}}
#### ⚙️ Parameters
{{#bodyParam}}
{{#isArray}}
{{#items}}
#### ⚙️ Parameter

{{> api_doc_param_data_type}}[]

{{#description}}
{{{description}}}

{{/description}}
{{/items}}
{{/isArray}}
{{/bodyParam}}
{{#allParamsWithRequestBodyProperties}}
{{#-first}}
#### ⚙️ Parameters

{{/-first}}
##### {{{paramName}}}: {{> api_doc_param_data_type}}

{{#description}}
Expand Down
Loading

0 comments on commit 387be7b

Please sign in to comment.