Skip to content

Commit b1176f5

Browse files
http-client-java, fix FinalResult model incorrectly removed (#9145)
Caused by #9109 Happens in mgmt flow, where we clean up unused model. Therefore, when we use FinalResult (instead of Response) model from LRO, we'd need to change this logic too. --- test Azure/autorest.java#3237 Azure/azure-sdk-for-java#47475 (some model get removed, Build pass)
1 parent 7af8f6d commit b1176f5

File tree

12 files changed

+458
-13
lines changed

12 files changed

+458
-13
lines changed

packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/transformer/SchemaCleanup.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ private static boolean tryCleanup(CodeModel codeModel, Set<String> javaNamesForP
108108
}
109109
if (!schemasNotInUse.isEmpty() || !choicesSchemasNotInUse.isEmpty()) {
110110
// operation responses
111-
Set<Schema> responses = codeModel.getOperationGroups()
112-
.stream()
113-
.flatMap(og -> og.getOperations().stream())
114-
.flatMap(o -> o.getResponses().stream())
115-
.map(Response::getSchema)
116-
.map(SchemaCleanup::schemaOrElementInCollection)
117-
.filter(Objects::nonNull)
118-
.collect(Collectors.toSet());
111+
Set<Schema> responses
112+
= codeModel.getOperationGroups().stream().flatMap(og -> og.getOperations().stream()).flatMap(o -> {
113+
if (o.getLroMetadata() == null) {
114+
// not LRO operation, or it is LRO but not from TypeSpec
115+
return o.getResponses().stream().map(Response::getSchema);
116+
} else {
117+
// if the operation has LroMetadata, SDK will use its FinalResultType
118+
return Stream.of(o.getLroMetadata().getFinalResultType());
119+
}
120+
}).map(SchemaCleanup::schemaOrElementInCollection).filter(Objects::nonNull).collect(Collectors.toSet());
119121
schemasNotInUse.removeAll(responses);
120122
choicesSchemasNotInUse.removeAll(responses);
121123
schemasInUse.addAll(responses);

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armresourceprovider/fluent/LroNoBodiesClient.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.ByteBuffer;
1515
import reactor.core.publisher.Flux;
1616
import reactor.core.publisher.Mono;
17+
import tsptest.armresourceprovider.models.ActionFinalResult;
1718
import tsptest.armresourceprovider.models.ResourceLroNoBody;
1819

1920
/**
@@ -126,4 +127,100 @@ ResourceLroNoBody createOrUpdate(String resourceGroupName, String resourceLroNoB
126127
@ServiceMethod(returns = ReturnType.SINGLE)
127128
ResourceLroNoBody createOrUpdate(String resourceGroupName, String resourceLroNoBodyName, ResourceLroNoBody resource,
128129
Context context);
130+
131+
/**
132+
* A long-running resource action.
133+
*
134+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
135+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
136+
* @throws IllegalArgumentException thrown if parameters fail the validation.
137+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
138+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
139+
* @return the response body along with {@link Response} on successful completion of {@link Mono}.
140+
*/
141+
@ServiceMethod(returns = ReturnType.SINGLE)
142+
Mono<Response<Flux<ByteBuffer>>> actionWithResponseAsync(String resourceGroupName, String resourceLroNoBodyName);
143+
144+
/**
145+
* A long-running resource action.
146+
*
147+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
148+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
149+
* @throws IllegalArgumentException thrown if parameters fail the validation.
150+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
151+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
152+
* @return the {@link PollerFlux} for polling of long-running operation.
153+
*/
154+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
155+
PollerFlux<PollResult<ActionFinalResult>, ActionFinalResult> beginActionAsync(String resourceGroupName,
156+
String resourceLroNoBodyName);
157+
158+
/**
159+
* A long-running resource action.
160+
*
161+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
162+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
163+
* @throws IllegalArgumentException thrown if parameters fail the validation.
164+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
165+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
166+
* @return the {@link SyncPoller} for polling of long-running operation.
167+
*/
168+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
169+
SyncPoller<PollResult<ActionFinalResult>, ActionFinalResult> beginAction(String resourceGroupName,
170+
String resourceLroNoBodyName);
171+
172+
/**
173+
* A long-running resource action.
174+
*
175+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
176+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
177+
* @param context The context to associate with this operation.
178+
* @throws IllegalArgumentException thrown if parameters fail the validation.
179+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
180+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
181+
* @return the {@link SyncPoller} for polling of long-running operation.
182+
*/
183+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
184+
SyncPoller<PollResult<ActionFinalResult>, ActionFinalResult> beginAction(String resourceGroupName,
185+
String resourceLroNoBodyName, Context context);
186+
187+
/**
188+
* A long-running resource action.
189+
*
190+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
191+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
192+
* @throws IllegalArgumentException thrown if parameters fail the validation.
193+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
194+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
195+
* @return the response body on successful completion of {@link Mono}.
196+
*/
197+
@ServiceMethod(returns = ReturnType.SINGLE)
198+
Mono<ActionFinalResult> actionAsync(String resourceGroupName, String resourceLroNoBodyName);
199+
200+
/**
201+
* A long-running resource action.
202+
*
203+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
204+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
205+
* @throws IllegalArgumentException thrown if parameters fail the validation.
206+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
207+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
208+
* @return the response.
209+
*/
210+
@ServiceMethod(returns = ReturnType.SINGLE)
211+
ActionFinalResult action(String resourceGroupName, String resourceLroNoBodyName);
212+
213+
/**
214+
* A long-running resource action.
215+
*
216+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
217+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
218+
* @param context The context to associate with this operation.
219+
* @throws IllegalArgumentException thrown if parameters fail the validation.
220+
* @throws com.azure.core.management.exception.ManagementException thrown if the request is rejected by server.
221+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
222+
* @return the response.
223+
*/
224+
@ServiceMethod(returns = ReturnType.SINGLE)
225+
ActionFinalResult action(String resourceGroupName, String resourceLroNoBodyName, Context context);
129226
}

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armresourceprovider/implementation/LroNoBodiesClientImpl.java

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.azure.core.annotation.Host;
1212
import com.azure.core.annotation.HostParam;
1313
import com.azure.core.annotation.PathParam;
14+
import com.azure.core.annotation.Post;
1415
import com.azure.core.annotation.Put;
1516
import com.azure.core.annotation.QueryParam;
1617
import com.azure.core.annotation.ReturnType;
@@ -30,6 +31,7 @@
3031
import reactor.core.publisher.Flux;
3132
import reactor.core.publisher.Mono;
3233
import tsptest.armresourceprovider.fluent.LroNoBodiesClient;
34+
import tsptest.armresourceprovider.models.ActionFinalResult;
3335
import tsptest.armresourceprovider.models.ResourceLroNoBody;
3436

3537
/**
@@ -85,6 +87,26 @@ Response<BinaryData> createOrUpdateSync(@HostParam("endpoint") String endpoint,
8587
@PathParam("resourceLroNoBodyName") String resourceLroNoBodyName,
8688
@HeaderParam("Content-Type") String contentType, @BodyParam("application/json") ResourceLroNoBody resource,
8789
Context context);
90+
91+
@Headers({ "Content-Type: application/json" })
92+
@Post("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/TspTest.ArmResourceProvider/resourceLroNoBody/{resourceLroNoBodyName}/action")
93+
@ExpectedResponses({ 200, 202 })
94+
@UnexpectedResponseExceptionType(ManagementException.class)
95+
Mono<Response<Flux<ByteBuffer>>> action(@HostParam("endpoint") String endpoint,
96+
@QueryParam("api-version") String apiVersion, @PathParam("subscriptionId") String subscriptionId,
97+
@PathParam("resourceGroupName") String resourceGroupName,
98+
@PathParam("resourceLroNoBodyName") String resourceLroNoBodyName, @HeaderParam("Accept") String accept,
99+
Context context);
100+
101+
@Headers({ "Content-Type: application/json" })
102+
@Post("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/TspTest.ArmResourceProvider/resourceLroNoBody/{resourceLroNoBodyName}/action")
103+
@ExpectedResponses({ 200, 202 })
104+
@UnexpectedResponseExceptionType(ManagementException.class)
105+
Response<BinaryData> actionSync(@HostParam("endpoint") String endpoint,
106+
@QueryParam("api-version") String apiVersion, @PathParam("subscriptionId") String subscriptionId,
107+
@PathParam("resourceGroupName") String resourceGroupName,
108+
@PathParam("resourceLroNoBodyName") String resourceLroNoBodyName, @HeaderParam("Accept") String accept,
109+
Context context);
88110
}
89111

90112
/**
@@ -261,4 +283,162 @@ public ResourceLroNoBody createOrUpdate(String resourceGroupName, String resourc
261283
ResourceLroNoBody resource, Context context) {
262284
return beginCreateOrUpdate(resourceGroupName, resourceLroNoBodyName, resource, context).getFinalResult();
263285
}
286+
287+
/**
288+
* A long-running resource action.
289+
*
290+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
291+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
292+
* @throws IllegalArgumentException thrown if parameters fail the validation.
293+
* @throws ManagementException thrown if the request is rejected by server.
294+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
295+
* @return the response body along with {@link Response} on successful completion of {@link Mono}.
296+
*/
297+
@ServiceMethod(returns = ReturnType.SINGLE)
298+
public Mono<Response<Flux<ByteBuffer>>> actionWithResponseAsync(String resourceGroupName,
299+
String resourceLroNoBodyName) {
300+
final String accept = "application/json";
301+
return FluxUtil
302+
.withContext(context -> service.action(this.client.getEndpoint(), this.client.getApiVersion(),
303+
this.client.getSubscriptionId(), resourceGroupName, resourceLroNoBodyName, accept, context))
304+
.contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
305+
}
306+
307+
/**
308+
* A long-running resource action.
309+
*
310+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
311+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
312+
* @throws IllegalArgumentException thrown if parameters fail the validation.
313+
* @throws ManagementException thrown if the request is rejected by server.
314+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
315+
* @return the response body along with {@link Response}.
316+
*/
317+
@ServiceMethod(returns = ReturnType.SINGLE)
318+
private Response<BinaryData> actionWithResponse(String resourceGroupName, String resourceLroNoBodyName) {
319+
final String accept = "application/json";
320+
return service.actionSync(this.client.getEndpoint(), this.client.getApiVersion(),
321+
this.client.getSubscriptionId(), resourceGroupName, resourceLroNoBodyName, accept, Context.NONE);
322+
}
323+
324+
/**
325+
* A long-running resource action.
326+
*
327+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
328+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
329+
* @param context The context to associate with this operation.
330+
* @throws IllegalArgumentException thrown if parameters fail the validation.
331+
* @throws ManagementException thrown if the request is rejected by server.
332+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
333+
* @return the response body along with {@link Response}.
334+
*/
335+
@ServiceMethod(returns = ReturnType.SINGLE)
336+
private Response<BinaryData> actionWithResponse(String resourceGroupName, String resourceLroNoBodyName,
337+
Context context) {
338+
final String accept = "application/json";
339+
return service.actionSync(this.client.getEndpoint(), this.client.getApiVersion(),
340+
this.client.getSubscriptionId(), resourceGroupName, resourceLroNoBodyName, accept, context);
341+
}
342+
343+
/**
344+
* A long-running resource action.
345+
*
346+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
347+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
348+
* @throws IllegalArgumentException thrown if parameters fail the validation.
349+
* @throws ManagementException thrown if the request is rejected by server.
350+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
351+
* @return the {@link PollerFlux} for polling of long-running operation.
352+
*/
353+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
354+
public PollerFlux<PollResult<ActionFinalResult>, ActionFinalResult> beginActionAsync(String resourceGroupName,
355+
String resourceLroNoBodyName) {
356+
Mono<Response<Flux<ByteBuffer>>> mono = actionWithResponseAsync(resourceGroupName, resourceLroNoBodyName);
357+
return this.client.<ActionFinalResult, ActionFinalResult>getLroResult(mono, this.client.getHttpPipeline(),
358+
ActionFinalResult.class, ActionFinalResult.class, this.client.getContext());
359+
}
360+
361+
/**
362+
* A long-running resource action.
363+
*
364+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
365+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
366+
* @throws IllegalArgumentException thrown if parameters fail the validation.
367+
* @throws ManagementException thrown if the request is rejected by server.
368+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
369+
* @return the {@link SyncPoller} for polling of long-running operation.
370+
*/
371+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
372+
public SyncPoller<PollResult<ActionFinalResult>, ActionFinalResult> beginAction(String resourceGroupName,
373+
String resourceLroNoBodyName) {
374+
Response<BinaryData> response = actionWithResponse(resourceGroupName, resourceLroNoBodyName);
375+
return this.client.<ActionFinalResult, ActionFinalResult>getLroResult(response, ActionFinalResult.class,
376+
ActionFinalResult.class, Context.NONE);
377+
}
378+
379+
/**
380+
* A long-running resource action.
381+
*
382+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
383+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
384+
* @param context The context to associate with this operation.
385+
* @throws IllegalArgumentException thrown if parameters fail the validation.
386+
* @throws ManagementException thrown if the request is rejected by server.
387+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
388+
* @return the {@link SyncPoller} for polling of long-running operation.
389+
*/
390+
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
391+
public SyncPoller<PollResult<ActionFinalResult>, ActionFinalResult> beginAction(String resourceGroupName,
392+
String resourceLroNoBodyName, Context context) {
393+
Response<BinaryData> response = actionWithResponse(resourceGroupName, resourceLroNoBodyName, context);
394+
return this.client.<ActionFinalResult, ActionFinalResult>getLroResult(response, ActionFinalResult.class,
395+
ActionFinalResult.class, context);
396+
}
397+
398+
/**
399+
* A long-running resource action.
400+
*
401+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
402+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
403+
* @throws IllegalArgumentException thrown if parameters fail the validation.
404+
* @throws ManagementException thrown if the request is rejected by server.
405+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
406+
* @return the response body on successful completion of {@link Mono}.
407+
*/
408+
@ServiceMethod(returns = ReturnType.SINGLE)
409+
public Mono<ActionFinalResult> actionAsync(String resourceGroupName, String resourceLroNoBodyName) {
410+
return beginActionAsync(resourceGroupName, resourceLroNoBodyName).last()
411+
.flatMap(this.client::getLroFinalResultOrError);
412+
}
413+
414+
/**
415+
* A long-running resource action.
416+
*
417+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
418+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
419+
* @throws IllegalArgumentException thrown if parameters fail the validation.
420+
* @throws ManagementException thrown if the request is rejected by server.
421+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
422+
* @return the response.
423+
*/
424+
@ServiceMethod(returns = ReturnType.SINGLE)
425+
public ActionFinalResult action(String resourceGroupName, String resourceLroNoBodyName) {
426+
return beginAction(resourceGroupName, resourceLroNoBodyName).getFinalResult();
427+
}
428+
429+
/**
430+
* A long-running resource action.
431+
*
432+
* @param resourceGroupName The name of the resource group. The name is case insensitive.
433+
* @param resourceLroNoBodyName The name of the ResourceLroNoBody.
434+
* @param context The context to associate with this operation.
435+
* @throws IllegalArgumentException thrown if parameters fail the validation.
436+
* @throws ManagementException thrown if the request is rejected by server.
437+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
438+
* @return the response.
439+
*/
440+
@ServiceMethod(returns = ReturnType.SINGLE)
441+
public ActionFinalResult action(String resourceGroupName, String resourceLroNoBodyName, Context context) {
442+
return beginAction(resourceGroupName, resourceLroNoBodyName, context).getFinalResult();
443+
}
264444
}

0 commit comments

Comments
 (0)