33
44package com .azure .autorest .template .example ;
55
6+ import com .azure .autorest .extension .base .model .codemodel .RequestParameterLocation ;
67import com .azure .autorest .model .clientmodel .ClassType ;
78import com .azure .autorest .model .clientmodel .ClientMethod ;
89import com .azure .autorest .model .clientmodel .ClientMethodParameter ;
4243import java .util .Set ;
4344import java .util .function .BiConsumer ;
4445import java .util .function .Consumer ;
46+ import java .util .function .Function ;
4547import java .util .stream .Collectors ;
4648
4749public class ClientMethodExampleWriter {
@@ -284,7 +286,7 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
284286 // output parameter's name is the "escaped reserved client method parameter name" of the real parameter's serialized name
285287 // since flattened parameter is always in body, we can deal with that explicitly
286288 ClientMethodParameter outputParameter = detail .getOutParameter ();
287- Map <String , Object > flattenedParameterValue = getFlattenedBodyParameterExampleValue (proxyMethodExample , outputParameter . getName () );
289+ Map <String , Object > flattenedParameterValue = getFlattenedBodyParameterExampleValue (proxyMethodExample , outputParameter );
288290 if (flattenedParameterValue != null ) {
289291 exampleValue .putAll (flattenedParameterValue );
290292 }
@@ -307,8 +309,8 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
307309 return ModelExampleUtil .parseNode (type , wireType , exampleValue );
308310 } else if (isFlattenParameter (convenienceMethod , methodParameter )) {
309311 // flatten, no grouping
310- String outputParameterName = convenienceMethod .getMethodTransformationDetails ().iterator ().next ().getOutParameter (). getName ();
311- Map <String , Object > realParameterValue = getFlattenedBodyParameterExampleValue (proxyMethodExample , outputParameterName );
312+ ClientMethodParameter outputParameter = convenienceMethod .getMethodTransformationDetails ().iterator ().next ().getOutParameter ();
313+ Map <String , Object > realParameterValue = getFlattenedBodyParameterExampleValue (proxyMethodExample , outputParameter );
312314
313315 IType type = methodParameter .getClientMethodParameter ().getClientType ();
314316 IType wireType = methodParameter .getClientMethodParameter ().getWireType ();
@@ -329,18 +331,25 @@ private ExampleNode parseNodeFromParameter(ClientMethod convenienceMethod, Proxy
329331 }
330332 }
331333
332- private Map <String , Object > getFlattenedBodyParameterExampleValue (ProxyMethodExample example , String clientMethodParameterName ) {
333- ProxyMethodExample .ParameterValue parameterValue = example .getParameters ().entrySet ()
334+ @ SuppressWarnings ("unchecked" )
335+ private Map <String , Object > getFlattenedBodyParameterExampleValue (ProxyMethodExample example , ClientMethodParameter clientMethodParameter ) {
336+ String clientMethodParameterName = clientMethodParameter .getName ();
337+ Function <String , ProxyMethodExample .ParameterValue > getParameterValue = (parameterSerializedName ) -> example .getParameters ().entrySet ()
334338 .stream ().filter (
335339 p -> CodeNamer .getEscapedReservedClientMethodParameterName (p .getKey ())
336- .equalsIgnoreCase (clientMethodParameterName ))
340+ .equalsIgnoreCase (parameterSerializedName ))
337341 .map (Map .Entry ::getValue )
338342 .findFirst ()
339343 .orElse (null );
340- if (parameterValue == null ) {
341- return null ;
344+ ProxyMethodExample .ParameterValue parameterValue = getParameterValue .apply (clientMethodParameterName );
345+
346+ if (parameterValue == null && clientMethodParameter .getRequestParameterLocation () == RequestParameterLocation .BODY && !"body" .equalsIgnoreCase (clientMethodParameterName )) {
347+ // fallback, "body" is commonly used in example JSON for request body
348+ clientMethodParameterName = "body" ;
349+ parameterValue = getParameterValue .apply (clientMethodParameterName );
342350 }
343- return (Map <String , Object >) parameterValue .getObjectValue ();
351+
352+ return parameterValue == null ? null : (Map <String , Object >) parameterValue .getObjectValue ();
344353 }
345354
346355 private boolean isGroupingParameter (ClientMethod convenienceMethod , MethodParameter methodParameter ) {
0 commit comments