-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sample generation for several mgmt plane RPs #1289
Changes from 16 commits
32fbafa
573b2aa
12e2d4b
ce03800
ee702a1
5a6a2ec
23724ef
2f439af
addf211
87dc7c0
503b59f
5cf0946
7959f8b
cc20dd1
efc7950
ba531a5
6a65ec5
79b7724
c473069
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ export interface SampleDetails { | |
isTopLevel: boolean, | ||
isPaging: boolean, | ||
originalFileLocation?: string | ||
isAnyTypeBody?: boolean, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice any usage for this column - isAnyTypeBody, does that mean we could delete it? |
||
importedTypes?: string[] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ import { calculateMethodName } from "../generators/utils/operationsUtils"; | |
import { camelCase } from "@azure-tools/codegen"; | ||
import { OperationGroupDetails } from "../models/operationDetails"; | ||
import { getPublicMethodName } from '../generators/utils/pagingOperations'; | ||
import { BodiedNode } from "ts-morph"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No usage delete this import? |
||
import { getTypeForSchema } from "../utils/schemaHelpers"; | ||
|
||
export async function transformSamples( | ||
codeModel: CodeModel, | ||
|
@@ -69,20 +71,22 @@ export async function getAllExamples(codeModel: TestCodeModel, clientDetails: Cl | |
example.operation.language | ||
).description, | ||
operationName: methodName, | ||
operationGroupName: normalizeName(opGroupName, NameType.Property), | ||
operationGroupName: normalizeName(opGroupName, NameType.Property, true), | ||
clientClassName: clientName, | ||
clientPackageName: packageDetails.name, | ||
clientParameterNames: "", | ||
methodParameterNames: "", | ||
bodySchemaName: "", | ||
isAnyTypeBody: false, | ||
hasBody: false, | ||
hasOptional: false, | ||
sampleFunctionName: camelCase(example.name.replace(/\//g, " Or ").replace(/,|\.|\(|\)/g, " ").replace('\'s ', ' ')), | ||
methodParamAssignments: [], | ||
clientParamAssignments: [], | ||
isTopLevel: ogDetails.isTopLevel, | ||
isPaging: opDetails.pagination !== undefined, | ||
originalFileLocation: example.originalFile | ||
originalFileLocation: example.originalFile, | ||
importedTypes: [] | ||
}; | ||
const clientParameterNames = ["credential"]; | ||
const requiredParams = clientDetails.parameters.filter( | ||
|
@@ -100,7 +104,8 @@ export async function getAllExamples(codeModel: TestCodeModel, clientDetails: Cl | |
} | ||
const parameterName = normalizeName( | ||
getLanguageMetadata(clientParameter.exampleValue.language).name, | ||
NameType.Parameter | ||
NameType.Parameter, | ||
true | ||
); | ||
const paramAssignment = | ||
`const ${parameterName} = ` + | ||
|
@@ -117,22 +122,31 @@ export async function getAllExamples(codeModel: TestCodeModel, clientDetails: Cl | |
sample.clientParameterNames = clientParameterNames.join(", "); | ||
} | ||
const methodParameterNames = []; | ||
const optionalParams = []; | ||
const optionalParams: [string, string][]= []; | ||
for (const methodParameter of example.methodParameters) { | ||
if ( | ||
methodParameter.exampleValue.schema.type === SchemaType.Constant | ||
) { | ||
continue; | ||
} | ||
const parameterName = getLanguageMetadata( | ||
methodParameter.exampleValue.language | ||
).name; | ||
const parameterName = normalizeName( | ||
getLanguageMetadata(methodParameter.exampleValue.language).name, | ||
NameType.Parameter, | ||
true | ||
); | ||
const parameterTypeDetails = getTypeForSchema( | ||
methodParameter.exampleValue.schema | ||
); | ||
const parameterTypeName = parameterTypeDetails.typeName; | ||
let paramAssignment = ""; | ||
if (methodParameter.parameter.protocol?.http?.["in"] === "body") { | ||
sample.hasBody = true; | ||
sample.bodySchemaName = getLanguageMetadata( | ||
methodParameter.exampleValue.schema.language | ||
).name; | ||
sample.bodySchemaName = parameterTypeName; | ||
sample.importedTypes?.push(parameterTypeName); | ||
if (methodParameter.exampleValue.schema.type === SchemaType.AnyObject || methodParameter.exampleValue.schema.type === SchemaType.Any) { | ||
sample.bodySchemaName = "Record<string, unknown>" | ||
sample.isAnyTypeBody = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We re-correct the value of bodySchemaName & isAnyTypeBody but we didn't put the latest into importedTypes list or any usage directly, do we need to refine this? |
||
} | ||
paramAssignment = | ||
`const ${parameterName}: ${sample.bodySchemaName} = ` + | ||
getParameterAssignment(methodParameter.exampleValue); | ||
|
@@ -142,7 +156,7 @@ export async function getAllExamples(codeModel: TestCodeModel, clientDetails: Cl | |
getParameterAssignment(methodParameter.exampleValue); | ||
} | ||
if (!methodParameter.parameter.required) { | ||
optionalParams.push(parameterName); | ||
optionalParams.push([ parameterName, parameterTypeName ]); | ||
} else { | ||
methodParameterNames.push(parameterName); | ||
} | ||
|
@@ -151,7 +165,10 @@ export async function getAllExamples(codeModel: TestCodeModel, clientDetails: Cl | |
if (optionalParams.length > 0) { | ||
const optionAssignment = `const options = {${optionalParams | ||
.map(item => { | ||
return item + ": " + item; | ||
if (sample.importedTypes?.indexOf(item[1]) === -1 && clientDetails.allTypes.indexOf(item[1]) > -1 ) { | ||
sample.importedTypes?.push(item[1]); | ||
} | ||
return item[0] + ": " + item[0] + " as " + item[1]; | ||
}) | ||
.join(", ")}}`; | ||
sample.methodParamAssignments.push(optionAssignment); | ||
|
@@ -193,6 +210,7 @@ function getParameterAssignment(exampleValue: ExampleValue) { | |
case SchemaType.Object: | ||
case SchemaType.Any: | ||
case SchemaType.Dictionary: | ||
case SchemaType.AnyObject: | ||
retValue = `{}`; | ||
break; | ||
case SchemaType.Array: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't use these two columns does that mean we could delete them in model and transform.ts?