Skip to content

Commit

Permalink
fix java namespace, git add src
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Aug 2, 2024
1 parent 23e8451 commit dc95160
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
52 changes: 31 additions & 21 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,7 @@ import {
modelIs,
pushDistinct,
} from "./type-utils.js";
import {
getJavaNamespace,
getNamespace,
logWarning,
pascalCase,
stringArrayContainsIgnoreCase,
trace,
} from "./utils.js";
import { getNamespace, logWarning, pascalCase, stringArrayContainsIgnoreCase, trace } from "./utils.js";
import { pathToFileURL } from "url";
const { isEqual } = pkg;

Expand Down Expand Up @@ -222,7 +215,7 @@ export class CodeModelBuilder {

this.namespace = getNamespaceFullName(serviceNamespace) || "Azure.Client";
// java namespace
const javaNamespace = getJavaNamespace(this.namespace);
const javaNamespace = this.getJavaNamespace(this.namespace);

const namespace1 = this.namespace;
this.typeNameOptions = {
Expand Down Expand Up @@ -532,12 +525,12 @@ export class CodeModelBuilder {

for (const client of clients) {
let clientName = client.name;
let javaNamespace = getJavaNamespace(this.namespace);
let javaNamespace = this.getJavaNamespace(this.namespace);
const clientFullName = client.name;
const clientNameSegments = clientFullName.split(".");
if (clientNameSegments.length > 1) {
clientName = clientNameSegments.at(-1)!;
javaNamespace = getJavaNamespace(this.namespace + "." + clientNameSegments.slice(0, -1).join("."));
javaNamespace = this.getJavaNamespace(this.namespace + "." + clientNameSegments.slice(0, -1).join("."));
}

const codeModelClient = new CodeModelClient(clientName, this.getDoc(client.type), {
Expand Down Expand Up @@ -925,7 +918,7 @@ export class CodeModelBuilder {
language: {
java: {
name: "OperationLocationPollingStrategy",
namespace: getJavaNamespace(this.namespace) + ".implementation",
namespace: this.getJavaNamespace(this.namespace) + ".implementation",
},
},
});
Expand Down Expand Up @@ -1491,7 +1484,7 @@ export class CodeModelBuilder {
namespace: namespace,
},
java: {
namespace: getJavaNamespace(namespace),
namespace: this.getJavaNamespace(namespace),
},
},
}),
Expand Down Expand Up @@ -1893,7 +1886,7 @@ export class CodeModelBuilder {
namespace: namespace,
},
java: {
namespace: getJavaNamespace(namespace),
namespace: this.getJavaNamespace(namespace),
},
},
});
Expand Down Expand Up @@ -1989,7 +1982,7 @@ export class CodeModelBuilder {
namespace: namespace,
},
java: {
namespace: getJavaNamespace(namespace),
namespace: this.getJavaNamespace(namespace),
},
},
});
Expand Down Expand Up @@ -2114,7 +2107,7 @@ export class CodeModelBuilder {
if (prop.kind === "property" && prop.multipartOptions) {
// TODO: handle MultipartOptions.isMulti
if (prop.multipartOptions.isFilePart) {
schema = this.processMultipartFormDataFilePropertySchemaFromSdkType(prop, this.namespace);
schema = this.processMultipartFormDataFilePropertySchemaFromSdkType(prop);
}
}

Expand Down Expand Up @@ -2155,7 +2148,7 @@ export class CodeModelBuilder {
namespace: namespace,
},
java: {
namespace: getJavaNamespace(namespace),
namespace: this.getJavaNamespace(namespace),
},
},
});
Expand Down Expand Up @@ -2233,14 +2226,14 @@ export class CodeModelBuilder {
}
}

private processMultipartFormDataFilePropertySchemaFromSdkType(
property: SdkBodyModelPropertyType,
namespace: string,
): Schema {
private processMultipartFormDataFilePropertySchemaFromSdkType(property: SdkBodyModelPropertyType): Schema {
const processSchemaFunc = (type: SdkType) => this.processSchemaFromSdkType(type, "");
if (property.type.kind === "bytes" || property.type.kind === "model") {
const namespace =
property.type.kind === "model" ? (getNamespace(property.type.__raw) ?? this.namespace) : this.namespace;
return getFileDetailsSchema(
property,
getNamespace(property.type.__raw) ?? this.namespace,
namespace,
this.codeModel.schemas,
this.binarySchema,
Expand All @@ -2251,12 +2244,17 @@ export class CodeModelBuilder {
property.type.kind === "array" &&
(property.type.valueType.kind === "bytes" || property.type.valueType.kind === "model")
) {
const namespace =
property.type.valueType.kind === "model"
? (getNamespace(property.type.valueType.__raw) ?? this.namespace)
: this.namespace;
return new ArraySchema(
property.name,
property.details ?? "",
getFileDetailsSchema(
property,
namespace,
this.getJavaNamespace(namespace),
this.codeModel.schemas,
this.binarySchema,
this.stringSchema,
Expand Down Expand Up @@ -2380,6 +2378,18 @@ export class CodeModelBuilder {
}
}

private getJavaNamespace(namespace: string | undefined): string | undefined {
const tspNamespace = this.namespace;
const baseJavaNamespace = this.emitterContext.options.namespace;
if (!namespace) {
return undefined;
} else if ((baseJavaNamespace && namespace === tspNamespace) || namespace.startsWith(tspNamespace + ".")) {
return baseJavaNamespace + namespace.slice(tspNamespace.length);
} else {
return "com." + namespace.toLowerCase();
}
}

private logWarning(msg: string) {
if (this.loggingEnabled) {
logWarning(this.program, msg);
Expand Down
17 changes: 12 additions & 5 deletions typespec-extension/src/external-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArraySchema, BinarySchema, ObjectSchema, Property, Schema, Schemas, StringSchema } from "@autorest/codemodel";
import { KnownMediaType } from "@azure-tools/codegen";
import { getJavaNamespace, getNamespace, pascalCase } from "./utils.js";
import { getNamespace, pascalCase } from "./utils.js";
import {
SdkBodyModelPropertyType,
SdkModelPropertyType,
Expand Down Expand Up @@ -126,14 +126,20 @@ function getFileSchemaName(baseName: string) {
}
}

function createFileDetailsSchema(schemaName: string, propertyName: string, namespace: string, schemas: Schemas) {
function createFileDetailsSchema(
schemaName: string,
propertyName: string,
namespace: string,
javaNamespace: string | undefined,
schemas: Schemas,
) {
const fileDetailsSchema = new ObjectSchema(schemaName, 'The file details for the "' + propertyName + '" field.', {
language: {
default: {
namespace: namespace,
},
java: {
namespace: getJavaNamespace(namespace),
namespace: javaNamespace,
},
},
serializationFormats: [KnownMediaType.Multipart],
Expand Down Expand Up @@ -201,6 +207,7 @@ function addContentTypeProperty(
export function getFileDetailsSchema(
property: SdkBodyModelPropertyType,
namespace: string,
javaNamespace: string | undefined,
schemas: Schemas,
binarySchema: BinarySchema,
stringSchema: StringSchema,
Expand Down Expand Up @@ -229,7 +236,7 @@ export function getFileDetailsSchema(
let fileDetailsSchema = fileDetailsMap.get(schemaName);
if (!fileDetailsSchema) {
const typeNamespace = getNamespace(property.type.__raw) ?? namespace;
fileDetailsSchema = createFileDetailsSchema(schemaName, filePropertyName, typeNamespace, schemas);
fileDetailsSchema = createFileDetailsSchema(schemaName, filePropertyName, typeNamespace, javaNamespace, schemas);

// description if available
if (fileSdkType.description) {
Expand Down Expand Up @@ -268,7 +275,7 @@ export function getFileDetailsSchema(
const schemaName = getFileSchemaName(filePropertyName);
let fileDetailsSchema = fileDetailsMap.get(schemaName);
if (!fileDetailsSchema) {
fileDetailsSchema = createFileDetailsSchema(schemaName, filePropertyName, namespace, schemas);
fileDetailsSchema = createFileDetailsSchema(schemaName, filePropertyName, namespace, javaNamespace, schemas);

addContentProperty(fileDetailsSchema, binarySchema);
addFilenameProperty(fileDetailsSchema, stringSchema);
Expand Down
4 changes: 0 additions & 4 deletions typespec-extension/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export function getNamespace(type: Type | undefined): string | undefined {
}
}

export function getJavaNamespace(namespace: string | undefined): string | undefined {
return namespace ? "com." + namespace.toLowerCase() : undefined;
}

export function stringArrayContainsIgnoreCase(stringList: string[], str: string): boolean {
return stringList && str ? stringList.findIndex((s) => s.toLowerCase() === str.toLowerCase()) != -1 : false;
}

0 comments on commit dc95160

Please sign in to comment.