Skip to content

Commit

Permalink
http-client-java, initial on unbranded pageable (#5410)
Browse files Browse the repository at this point in the history
link #5350

target https://github.com/Azure/azure-sdk-for-java/pull/43484/files

first step to get most things ready.

detail still depends on clientcore API

locally tested against the azure/payload/pageable case
  • Loading branch information
weidongxu-microsoft authored Dec 19, 2024
1 parent cf8d1f2 commit 8214ed6
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 66 deletions.
7 changes: 5 additions & 2 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2562,8 +2562,11 @@ export class CodeModelBuilder {
return this.baseJavaNamespace;
}
} else {
// special handling for namespace of model in TypeSpec.Rest.Resource
if (type.crossLanguageDefinitionId.startsWith("TypeSpec.Rest.Resource.")) {
// special handling for namespace of model in TypeSpec
if (type.crossLanguageDefinitionId === "TypeSpec.Http.File") {
// TypeSpec.Http.File
return this.baseJavaNamespace;
} else if (type.crossLanguageDefinitionId.startsWith("TypeSpec.Rest.Resource.")) {
// models in TypeSpec.Rest.Resource
return this.baseJavaNamespace;
}
Expand Down
13 changes: 10 additions & 3 deletions packages/http-client-java/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { dump } from "js-yaml";
import { dirname } from "path";
import { fileURLToPath } from "url";
import { CodeModelBuilder } from "./code-model-builder.js";
import { CodeModel } from "./common/code-model.js";
import { logError, spawnAsync } from "./utils.js";
import { JDK_NOT_FOUND_MESSAGE, validateDependencies } from "./validate.js";

Expand Down Expand Up @@ -125,10 +126,16 @@ export async function $onEmit(context: EmitContext<EmitterOptions>) {
options["flavor"] = "azure";
}
}
const builder = new CodeModelBuilder(program, context);
const codeModel = await builder.build();

if (!program.hasError() && !program.compilerOptions.noEmit) {
let codeModel: CodeModel | undefined;
try {
const builder = new CodeModelBuilder(program, context);
codeModel = await builder.build();
} catch (error: any) {
logError(program, error.message);
}

if (codeModel && !program.hasError() && !program.compilerOptions.noEmit) {
const __dirname = dirname(fileURLToPath(import.meta.url));
const moduleRoot = resolvePath(__dirname, "..", "..");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,10 @@ private List<ClientMethod> createClientMethods(Operation operation, boolean isPr
// in this case, it is not original parameter from any other parameters
for (Parameter parameter : request.getParameters()
.stream()
.filter(p -> p.isFlattened() && p.getProtocol() != null && p.getProtocol().getHttp() != null) // flattened
// proxy
// parameter
.filter(p -> !originalParameters.contains(p)) // but
// not
// original
// parameter
// from
// any
// other
// parameters
// flattened proxy parameter
.filter(p -> p.isFlattened() && p.getProtocol() != null && p.getProtocol().getHttp() != null)
// but not original parameter from any other parameters
.filter(p -> !originalParameters.contains(p))
.collect(Collectors.toList())) {
ClientMethodParameter outParameter = Mappers.getClientParameterMapper().map(parameter);
methodTransformationDetails.add(new MethodTransformationDetail(outParameter, new ArrayList<>()));
Expand Down Expand Up @@ -616,7 +609,8 @@ private ReturnTypeHolder getReturnTypes(Operation operation, boolean isProtocolM
.ifPresentOrElse(itemProperty -> {
IType listType = itemProperty.getWireType();
IType elementType = ((ListType) listType).getElementType();
if (isProtocolMethod) {
// unbranded would use the model, instead of BinaryData, as return type
if (isProtocolMethod && settings.isBranded()) {
returnTypeHolder.asyncRestResponseReturnType = createProtocolPagedRestResponseReturnType();
returnTypeHolder.asyncReturnType = createProtocolPagedAsyncReturnType();
returnTypeHolder.syncReturnType = createProtocolPagedSyncReturnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public Map<Request, List<ProxyMethod>> map(Operation operation) {
builder.responseExpectedStatusCodes(expectedStatusCodes);

IType responseBodyType = MapperUtils.handleResponseSchema(operation, settings);
// unbranded would use the model, instead of BinaryData, as return type
if (settings.isDataPlaneClient() && settings.isBranded()) {
builder.rawResponseBodyType(responseBodyType);
responseBodyType = SchemaUtil.removeModelFromResponse(responseBodyType, operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ private static ClassType.Builder getClassTypeBuilder(Class<?> classKey) {
public static final ClassType INPUT_STREAM = new ClassType.Builder(false).knownClass(InputStream.class).build();

public static final ClassType CONTEXT = ClassType.getClassTypeBuilder(Context.class)
.defaultValueExpressionConverter(epr -> "com.azure.core.util.Context.NONE")
.defaultValueExpressionConverter(
epr -> (JavaSettings.getInstance().isBranded() ? "com.azure.core.util." : "io.clientcore.core.util.")
+ TemplateUtil.getContextNone())
.build();

public static final ClassType ANDROID_CONTEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,23 @@ public static GenericType RestResponse(IType headersType, IType bodyType) {
}

public static GenericType PagedResponse(IType bodyType) {
return new GenericType("com.azure.core.http.rest", "PagedResponse", bodyType);
if (JavaSettings.getInstance().isBranded()) {
return new GenericType("com.azure.core.http.rest", "PagedResponse", bodyType);
} else {
return new GenericType("io.clientcore.core.http.models", "PagedResponse", bodyType);
}
}

public static GenericType PagedFlux(IType bodyType) {
return new GenericType("com.azure.core.http.rest", "PagedFlux", bodyType);
}

public static GenericType PagedIterable(IType bodyType) {
return new GenericType("com.azure.core.http.rest", "PagedIterable", bodyType);
if (JavaSettings.getInstance().isBranded()) {
return new GenericType("com.azure.core.http.rest", "PagedIterable", bodyType);
} else {
return new GenericType("io.clientcore.core.http.models", "PagedIterable", bodyType);
}
}

public static GenericType Function(IType inputType, IType outputType) {
Expand Down
Loading

0 comments on commit 8214ed6

Please sign in to comment.