Skip to content

Commit

Permalink
Ensure code model types don't have overlapping shapes (#1382)
Browse files Browse the repository at this point in the history
This can lead to some subtle ordering issues, leading to conditions that
can never be true.
  • Loading branch information
jhendrixMSFT authored Jun 12, 2024
1 parent 4802b84 commit 6fdf1d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/codegen.go/src/fake/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ function dispatchForOperationBody(clientPkg: string, receiverName: string, metho

const isMultipartContentType = function(type: go.PossibleType): type is go.QualifiedType {
type = helpers.recursiveUnwrapMapSlice(type);
return (go.isQualifiedType(type) && type.typeName === 'MultipartContent');
return (go.isQualifiedType(type) && type.exportName === 'MultipartContent');
};

const emitCase = function(caseValue: string, paramVar: string, type: go.PossibleType): string {
Expand All @@ -472,7 +472,7 @@ function dispatchForOperationBody(clientPkg: string, receiverName: string, metho
if (go.isModelType(helpers.recursiveUnwrapMapSlice(type))) {
imports.add('encoding/json');
caseContent += `\t\t\tif err = json.Unmarshal(content, &${paramVar}); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n`;
} else if (go.isQualifiedType(type) && type.typeName === 'ReadSeekCloser') {
} else if (go.isQualifiedType(type) && type.exportName === 'ReadSeekCloser') {
imports.add('bytes');
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
assignedValue = 'streaming.NopCloser(bytes.NewReader(content))';
Expand Down Expand Up @@ -532,7 +532,7 @@ function dispatchForOperationBody(clientPkg: string, receiverName: string, metho
caseContent += `\t\t\t${paramVar}.Filename = ${filename}\n`;
}
} else if (go.isSliceType(type)) {
if (go.isQualifiedType(type.elementType) && type.elementType.typeName === 'ReadSeekCloser') {
if (go.isQualifiedType(type.elementType) && type.elementType.exportName === 'ReadSeekCloser') {
imports.add('bytes');
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
assignedValue = `append(${paramVar}, streaming.NopCloser(bytes.NewReader(content)))`;
Expand Down
22 changes: 11 additions & 11 deletions packages/codemodel.go/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export interface LiteralValue {
// QualifiedType is a type from some package, e.g. the Go standard library (excluding time.Time)
export interface QualifiedType {
// this is the type name minus any package qualifier (e.g. URL)
typeName: string;
exportName: string;

// the full name of the package to import (e.g. "net/url")
packageName: string;
Expand All @@ -172,10 +172,6 @@ export type DateTimeFormat = 'dateType' | 'dateTimeRFC1123' | 'dateTimeRFC3339'

// TimeType is a time.Time type from the standard library with a format specifier.
export interface TimeType {
typeName: 'Time';

packageName: 'time';

dateTimeFormat: DateTimeFormat;

utc: boolean;
Expand Down Expand Up @@ -247,11 +243,11 @@ export function isLiteralValueType(type: PossibleType): type is LiteralValueType
}

export function isPrimitiveType(type: PossibleType): type is PrimitiveType {
return (<PrimitiveType>type).typeName !== undefined && !isQualifiedType(type) && !isTimeType(type);
return (<PrimitiveType>type).typeName !== undefined;
}

export function isQualifiedType(type: PossibleType): type is QualifiedType {
return (<QualifiedType>type).packageName !== undefined && !isTimeType(type);
return (<QualifiedType>type).exportName !== undefined;
}

export function isTimeType(type: PossibleType): type is TimeType {
Expand Down Expand Up @@ -287,8 +283,12 @@ export function getLiteralValueTypeName(literal: LiteralValueType): string {
return '[]byte';
} else if (isConstantType(literal)) {
return literal.name;
} else {
} else if (isPrimitiveType(literal)) {
return literal.typeName;
} else if (isTimeType(literal)) {
return 'time.Time';
} else {
throw new Error(`unhandled LiteralValueType ${getTypeDeclaration(literal)}`);
}
}

Expand All @@ -301,7 +301,7 @@ export function getTypeDeclaration(type: PossibleType, pkgName?: string): string
if (pathChar) {
pkg = pkg.substring(pathChar+1);
}
return pkg + '.' + type.typeName;
return pkg + '.' + type.exportName;
} else if (isConstantType(type) || isInterfaceType(type) || isModelType(type) || isPolymorphicType(type)) {
if (pkgName) {
return `${pkgName}.${type.name}`;
Expand Down Expand Up @@ -445,8 +445,8 @@ export class PrimitiveType implements PrimitiveType {
}

export class QualifiedType implements QualifiedType {
constructor(typeName: string, packageName: string) {
this.typeName = typeName;
constructor(exportName: string, packageName: string) {
this.exportName = exportName;
this.packageName = packageName;
}
}
Expand Down

0 comments on commit 6fdf1d0

Please sign in to comment.