Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.Type;
import org.slf4j.Logger;

import java.util.Arrays;
Expand Down Expand Up @@ -44,14 +45,14 @@ public void customize(LibraryCustomization libraryCustomization, Logger logger)
removeGetApis(searchClient);
removeGetApis(searchAsyncClient);

hideResponseBinaryDataApis(searchClient);
hideResponseBinaryDataApis(searchAsyncClient);
hideResponseBinaryDataApis(indexes.getClass("SearchIndexClient"));
hideResponseBinaryDataApis(indexes.getClass("SearchIndexAsyncClient"));
hideResponseBinaryDataApis(indexes.getClass("SearchIndexerClient"));
hideResponseBinaryDataApis(indexes.getClass("SearchIndexerAsyncClient"));
hideResponseBinaryDataApis(knowledge.getClass("KnowledgeBaseRetrievalClient"));
hideResponseBinaryDataApis(knowledge.getClass("KnowledgeBaseRetrievalAsyncClient"));
hideWithResponseBinaryDataApis(searchClient);
hideWithResponseBinaryDataApis(searchAsyncClient);
hideWithResponseBinaryDataApis(indexes.getClass("SearchIndexClient"));
hideWithResponseBinaryDataApis(indexes.getClass("SearchIndexAsyncClient"));
hideWithResponseBinaryDataApis(indexes.getClass("SearchIndexerClient"));
hideWithResponseBinaryDataApis(indexes.getClass("SearchIndexerAsyncClient"));
hideWithResponseBinaryDataApis(knowledge.getClass("KnowledgeBaseRetrievalClient"));
hideWithResponseBinaryDataApis(knowledge.getClass("KnowledgeBaseRetrievalAsyncClient"));
}

// Weird quirk in the Java generator where SearchOptions is inferred from the parameters of searchPost in TypeSpec,
Expand Down Expand Up @@ -121,20 +122,29 @@ private static void includeOldApiVersions(ClassCustomization customization) {
}));
}

// At the time this was added, Java TypeSpec for Azure-type generation doesn't support returning Response<T>, which
// we want, so hide all the Response<BinaryData> APIs in the specified class and manually add Response<T> APIs.
private static void hideResponseBinaryDataApis(ClassCustomization customization) {
// At the time this was added, Java TypeSpec for Azure-type generation doesn't use 'T' in WithResponse APIs, which
// we want, so hide all the WithResponse APIs using BinaryData in the specified class and manually add 'T' APIs.
private static void hideWithResponseBinaryDataApis(ClassCustomization customization) {
customization.customizeAst(ast -> ast.getClassByName(customization.getClassName())
.ifPresent(clazz -> clazz.getMethods().forEach(method -> {
if (method.isPublic()
&& method.isAnnotationPresent("Generated")
&& method.getNameAsString().endsWith("WithResponse")
&& method.getType().toString().contains("Response<BinaryData>")) {
if (!method.isPublic() || !method.isAnnotationPresent("Generated")) {
// Method either isn't public or isn't Generated, skip deeper inspection.
return;
}

if (hasBinaryDataInType(method.getType())
|| method.getParameters().stream().anyMatch(param -> hasBinaryDataInType(param.getType()))) {
String methodName = method.getNameAsString();
Comment thread
alzimmermsft marked this conversation as resolved.
String newMethodName = "hiddenGenerated" + Character.toLowerCase(methodName.charAt(0))
String newMethodName = "hiddenGenerated" + Character.toUpperCase(methodName.charAt(0))
+ methodName.substring(1);
method.setModifiers().setName(newMethodName);

String returnTypeName = method.getType().toString();
if (returnTypeName.contains("PagedIterable")) {
// PagedIterable generation behaves differently and will break with the logic below.
return;
}

clazz.getMethodsByName(methodName.replace("WithResponse", "")).forEach(nonWithResponse -> {
String body = nonWithResponse.getBody().map(BlockStmt::toString).get();
body = body.replace(methodName, newMethodName);
Expand All @@ -144,6 +154,10 @@ private static void hideResponseBinaryDataApis(ClassCustomization customization)
})));
}

private static boolean hasBinaryDataInType(Type type) {
return type.toString().contains("BinaryData");
}

// Removes GET equivalents of POST APIs in SearchClient and SearchAsyncClient as we never plan to expose those.
private static void removeGetApis(ClassCustomization customization) {
List<String> methodPrefixesToRemove = Arrays.asList("searchGet", "suggestGet", "autocompleteGet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ Mono<Response<BinaryData>> indexWithResponse(BinaryData batch, RequestOptions re
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Long> getDocumentCount() {
// Generated convenience method for hiddenGeneratedgetDocumentCountWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentCountWithResponse
RequestOptions requestOptions = new RequestOptions();
return hiddenGeneratedgetDocumentCountWithResponse(requestOptions).flatMap(FluxUtil::toMono)
return hiddenGeneratedGetDocumentCountWithResponse(requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(Long.class));
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public SearchPagedFlux search(SearchOptions options, RequestOptions requestOptio
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LookupDocument> getDocument(String key, String querySourceAuthorization, Boolean enableElevatedRead,
List<String> selectedFields) {
// Generated convenience method for hiddenGeneratedgetDocumentWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentWithResponse
RequestOptions requestOptions = new RequestOptions();
if (querySourceAuthorization != null) {
requestOptions.setHeader(HttpHeaderName.fromString("x-ms-query-source-authorization"),
Expand All @@ -276,7 +276,7 @@ public Mono<LookupDocument> getDocument(String key, String querySourceAuthorizat
.collect(Collectors.joining(",")),
false);
}
return hiddenGeneratedgetDocumentWithResponse(key, requestOptions).flatMap(FluxUtil::toMono)
return hiddenGeneratedGetDocumentWithResponse(key, requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(LookupDocument.class));
}

Expand All @@ -295,9 +295,9 @@ public Mono<LookupDocument> getDocument(String key, String querySourceAuthorizat
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LookupDocument> getDocument(String key) {
// Generated convenience method for hiddenGeneratedgetDocumentWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentWithResponse
RequestOptions requestOptions = new RequestOptions();
return hiddenGeneratedgetDocumentWithResponse(key, requestOptions).flatMap(FluxUtil::toMono)
return hiddenGeneratedGetDocumentWithResponse(key, requestOptions).flatMap(FluxUtil::toMono)
.map(protocolMethodData -> protocolMethodData.toObject(LookupDocument.class));
}

Expand Down Expand Up @@ -985,7 +985,7 @@ public Mono<Response<LookupDocument>> getDocumentWithResponse(String key, Reques
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<BinaryData>> hiddenGeneratedgetDocumentCountWithResponse(RequestOptions requestOptions) {
Mono<Response<BinaryData>> hiddenGeneratedGetDocumentCountWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getDocumentCountWithResponseAsync(requestOptions);
}

Expand Down Expand Up @@ -1033,7 +1033,7 @@ Mono<Response<BinaryData>> hiddenGeneratedgetDocumentCountWithResponse(RequestOp
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<BinaryData>> hiddenGeneratedgetDocumentWithResponse(String key, RequestOptions requestOptions) {
Mono<Response<BinaryData>> hiddenGeneratedGetDocumentWithResponse(String key, RequestOptions requestOptions) {
return this.serviceClient.getDocumentWithResponseAsync(key, requestOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ Response<BinaryData> indexWithResponse(BinaryData batch, RequestOptions requestO
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public long getDocumentCount() {
// Generated convenience method for hiddenGeneratedgetDocumentCountWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentCountWithResponse
RequestOptions requestOptions = new RequestOptions();
return hiddenGeneratedgetDocumentCountWithResponse(requestOptions).getValue().toObject(Long.class);
return hiddenGeneratedGetDocumentCountWithResponse(requestOptions).getValue().toObject(Long.class);
}

/**
Expand Down Expand Up @@ -263,7 +263,7 @@ public SearchPagedIterable search(SearchOptions options, RequestOptions requestO
@ServiceMethod(returns = ReturnType.SINGLE)
public LookupDocument getDocument(String key, String querySourceAuthorization, Boolean enableElevatedRead,
List<String> selectedFields) {
// Generated convenience method for hiddenGeneratedgetDocumentWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentWithResponse
RequestOptions requestOptions = new RequestOptions();
if (querySourceAuthorization != null) {
requestOptions.setHeader(HttpHeaderName.fromString("x-ms-query-source-authorization"),
Expand All @@ -280,7 +280,7 @@ public LookupDocument getDocument(String key, String querySourceAuthorization, B
.collect(Collectors.joining(",")),
false);
}
return hiddenGeneratedgetDocumentWithResponse(key, requestOptions).getValue().toObject(LookupDocument.class);
return hiddenGeneratedGetDocumentWithResponse(key, requestOptions).getValue().toObject(LookupDocument.class);
}

/**
Expand All @@ -298,9 +298,9 @@ public LookupDocument getDocument(String key, String querySourceAuthorization, B
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
public LookupDocument getDocument(String key) {
// Generated convenience method for hiddenGeneratedgetDocumentWithResponse
// Generated convenience method for hiddenGeneratedGetDocumentWithResponse
RequestOptions requestOptions = new RequestOptions();
return hiddenGeneratedgetDocumentWithResponse(key, requestOptions).getValue().toObject(LookupDocument.class);
return hiddenGeneratedGetDocumentWithResponse(key, requestOptions).getValue().toObject(LookupDocument.class);
}

/**
Expand Down Expand Up @@ -980,7 +980,7 @@ public Response<LookupDocument> getDocumentWithResponse(String key, RequestOptio
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Response<BinaryData> hiddenGeneratedgetDocumentCountWithResponse(RequestOptions requestOptions) {
Response<BinaryData> hiddenGeneratedGetDocumentCountWithResponse(RequestOptions requestOptions) {
return this.serviceClient.getDocumentCountWithResponse(requestOptions);
}

Expand Down Expand Up @@ -1027,7 +1027,7 @@ Response<BinaryData> hiddenGeneratedgetDocumentCountWithResponse(RequestOptions
*/
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Response<BinaryData> hiddenGeneratedgetDocumentWithResponse(String key, RequestOptions requestOptions) {
Response<BinaryData> hiddenGeneratedGetDocumentWithResponse(String key, RequestOptions requestOptions) {
return this.serviceClient.getDocumentWithResponse(key, requestOptions);
}
}
Loading
Loading