Skip to content

Commit

Permalink
Don't treat optional, constant, body params as required (#981)
Browse files Browse the repository at this point in the history
This makes the behavior consistent with how optional, constant header
and query parameters are handled.
  • Loading branch information
jhendrixMSFT authored Jun 28, 2023
1 parent 938e428 commit e44c577
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 85 deletions.
4 changes: 2 additions & 2 deletions packages/autorest.go/src/generator/fake/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,14 @@ function dispatchForOperationBody(clientPkg: string, receiverName: string, op: O
// nothing to do for binary media type
} else if (mediaType === 'Text') {
const bodyParam = values(aggregateParameters(op)).where((each: Parameter) => { return each.protocol.http?.in === 'body'; }).first();
if (bodyParam && bodyParam.schema.type !== SchemaType.Constant) {
if (bodyParam && !(bodyParam.required === true && bodyParam.schema.type === SchemaType.Constant)) {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/fake', 'azfake');
content += '\tbody, err := server.UnmarshalRequestAsText(req)\n';
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
}
} else if (mediaType === 'JSON' || mediaType === 'XML') {
const bodyParam = values(aggregateParameters(op)).where((each: Parameter) => { return each.protocol.http?.in === 'body'; }).first();
if (bodyParam && bodyParam.schema.type !== SchemaType.Constant) {
if (bodyParam && !(bodyParam.required === true && bodyParam.schema.type === SchemaType.Constant)) {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/fake', 'azfake');
if (bodyParam.schema.type === SchemaType.ByteArray) {
let format = 'Std';
Expand Down
2 changes: 1 addition & 1 deletion packages/autorest.go/src/generator/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ function createProtocolRequest(group: OperationGroup, op: Operation, imports: Im
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
setBody = `req.SetBody(streaming.NopCloser(bytes.NewReader(${body})), "application/${mediaType.toLowerCase()}")`;
}
if (bodyParam!.required || bodyParam!.schema.type === SchemaType.Constant) {
if (bodyParam!.required) {
text += `\t${emitSetBodyWithErrCheck(setBody)}`;
text += '\treturn req, nil\n';
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/autorest.go/src/transform/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ async function processOperationRequests(session: Session<CodeModel>) {
} else if (dupe.schema !== param.schema) {
throw new Error(`parameter group ${paramGroupName} contains overlapping parameters with different schemas`);
}
} else if (param.implementation === ImplementationLocation.Method && param.required !== true && !(param.schema.type === SchemaType.Constant && param.protocol.http!.in === 'body')) {
// include non-required constants that aren't body params in the optional values struct.
} else if (param.implementation === ImplementationLocation.Method && param.required !== true) {
// include all non-required method params in the optional values struct.
(<GroupProperty>op.language.go!.optionalParamGroup).originalParameter.push(param);
// associate the group with the param
param.language.go!.paramGroup = op.language.go!.optionalParamGroup;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e44c577

Please sign in to comment.