Skip to content

Commit 4fe6c26

Browse files
authored
Fixes for multi-level discriminators (#2799)
* Add new TypeSpec for testing * Fix nested enum discriminators
1 parent b804754 commit 4fe6c26

24 files changed

+2569
-1
lines changed

javagen/src/main/java/com/azure/autorest/mapper/ModelMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ public ClientModel map(ObjectSchema compositeType) {
373373
if (!Objects.equals(polymorphicDiscriminator, derivedType.getPolymorphicDiscriminatorName())) {
374374
ClientModelProperty parentDiscriminator = result.getPolymorphicDiscriminator()
375375
.newBuilder()
376-
.defaultValue(derivedType.getPolymorphicDiscriminator().getDefaultValue())
376+
.defaultValue(result.getPolymorphicDiscriminator().getClientType()
377+
.defaultValueExpression(derivedType.getSerializedName()))
377378
.build();
378379

379380
passPolymorphicDiscriminatorToChildren(parentDiscriminator, derivedType);

javagen/src/main/java/com/azure/autorest/template/ModelTemplate.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ public final void write(ClientModel model, JavaFile javaFile) {
158158
: JavaVisibility.Public;
159159
addModelConstructor(model, modelConstructorVisibility, settings, classBlock);
160160

161+
// Getters for parent discriminator properties
162+
for (ClientModelProperty property : model.getParentPolymorphicDiscriminators()) {
163+
IType propertyWireType = property.getWireType();
164+
IType propertyClientType = propertyWireType.getClientType();
165+
166+
generateGetterJavadoc(classBlock, property);
167+
addGeneratedAnnotation(classBlock);
168+
classBlock.annotation("Override");
169+
classBlock.method(JavaVisibility.Public, null,
170+
propertyClientType + " " + getGetterName(model, property) + "()",
171+
methodBlock -> addGetterMethod(propertyWireType, propertyClientType, property, treatAsXml,
172+
methodBlock, settings));
173+
}
174+
161175
for (ClientModelProperty property : model.getProperties()) {
162176
final boolean propertyIsReadOnly = immutableOutputModel || property.isReadOnly();
163177

typespec-tests/src/main/java/com/cadl/patch/models/SawShark.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public final class SawShark extends Shark {
4444
public SawShark() {
4545
}
4646

47+
/**
48+
* Get the kind property: The kind property.
49+
*
50+
* @return the kind value.
51+
*/
52+
@Generated
53+
@Override
54+
public String getKind() {
55+
return this.kind;
56+
}
57+
4758
/**
4859
* Get the sharktype property: The sharktype property.
4960
*

typespec-tests/src/main/java/com/cadl/patch/models/Shark.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public void setWeight(Shark model, Integer weight) {
5959
public Shark() {
6060
}
6161

62+
/**
63+
* Get the kind property: The kind property.
64+
*
65+
* @return the kind value.
66+
*/
67+
@Generated
68+
@Override
69+
public String getKind() {
70+
return this.kind;
71+
}
72+
6273
/**
6374
* Get the sharktype property: The sharktype property.
6475
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
// Code generated by Microsoft (R) TypeSpec Code Generator.
4+
5+
package com.type.model.inheritance.enumnesteddiscriminator;
6+
7+
import com.azure.core.annotation.Generated;
8+
import com.azure.core.annotation.ReturnType;
9+
import com.azure.core.annotation.ServiceClient;
10+
import com.azure.core.annotation.ServiceMethod;
11+
import com.azure.core.exception.ClientAuthenticationException;
12+
import com.azure.core.exception.HttpResponseException;
13+
import com.azure.core.exception.ResourceModifiedException;
14+
import com.azure.core.exception.ResourceNotFoundException;
15+
import com.azure.core.http.rest.RequestOptions;
16+
import com.azure.core.http.rest.Response;
17+
import com.azure.core.util.BinaryData;
18+
import com.azure.core.util.FluxUtil;
19+
import com.type.model.inheritance.enumnesteddiscriminator.implementation.EnumNestedDiscriminatorClientImpl;
20+
import com.type.model.inheritance.enumnesteddiscriminator.models.Fish;
21+
import reactor.core.publisher.Mono;
22+
23+
/**
24+
* Initializes a new instance of the asynchronous EnumNestedDiscriminatorClient type.
25+
*/
26+
@ServiceClient(builder = EnumNestedDiscriminatorClientBuilder.class, isAsync = true)
27+
public final class EnumNestedDiscriminatorAsyncClient {
28+
@Generated
29+
private final EnumNestedDiscriminatorClientImpl serviceClient;
30+
31+
/**
32+
* Initializes an instance of EnumNestedDiscriminatorAsyncClient class.
33+
*
34+
* @param serviceClient the service client implementation.
35+
*/
36+
@Generated
37+
EnumNestedDiscriminatorAsyncClient(EnumNestedDiscriminatorClientImpl serviceClient) {
38+
this.serviceClient = serviceClient;
39+
}
40+
41+
/**
42+
* The getModel operation.
43+
* <p><strong>Response Body Schema</strong></p>
44+
*
45+
* <pre>{@code
46+
* {
47+
* kind: String(shark/salmon) (Required)
48+
* age: int (Required)
49+
* }
50+
* }</pre>
51+
*
52+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
53+
* @throws HttpResponseException thrown if the request is rejected by server.
54+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
55+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
56+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
57+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator along with
58+
* {@link Response} on successful completion of {@link Mono}.
59+
*/
60+
@Generated
61+
@ServiceMethod(returns = ReturnType.SINGLE)
62+
public Mono<Response<BinaryData>> getModelWithResponse(RequestOptions requestOptions) {
63+
return this.serviceClient.getModelWithResponseAsync(requestOptions);
64+
}
65+
66+
/**
67+
* The putModel operation.
68+
* <p><strong>Request Body Schema</strong></p>
69+
*
70+
* <pre>{@code
71+
* {
72+
* kind: String(shark/salmon) (Required)
73+
* age: int (Required)
74+
* }
75+
* }</pre>
76+
*
77+
* @param input The input parameter.
78+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
79+
* @throws HttpResponseException thrown if the request is rejected by server.
80+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
81+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
82+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
83+
* @return the {@link Response} on successful completion of {@link Mono}.
84+
*/
85+
@Generated
86+
@ServiceMethod(returns = ReturnType.SINGLE)
87+
public Mono<Response<Void>> putModelWithResponse(BinaryData input, RequestOptions requestOptions) {
88+
return this.serviceClient.putModelWithResponseAsync(input, requestOptions);
89+
}
90+
91+
/**
92+
* The getRecursiveModel operation.
93+
* <p><strong>Response Body Schema</strong></p>
94+
*
95+
* <pre>{@code
96+
* {
97+
* kind: String(shark/salmon) (Required)
98+
* age: int (Required)
99+
* }
100+
* }</pre>
101+
*
102+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
103+
* @throws HttpResponseException thrown if the request is rejected by server.
104+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
105+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
106+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
107+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator along with
108+
* {@link Response} on successful completion of {@link Mono}.
109+
*/
110+
@Generated
111+
@ServiceMethod(returns = ReturnType.SINGLE)
112+
public Mono<Response<BinaryData>> getRecursiveModelWithResponse(RequestOptions requestOptions) {
113+
return this.serviceClient.getRecursiveModelWithResponseAsync(requestOptions);
114+
}
115+
116+
/**
117+
* The putRecursiveModel operation.
118+
* <p><strong>Request Body Schema</strong></p>
119+
*
120+
* <pre>{@code
121+
* {
122+
* kind: String(shark/salmon) (Required)
123+
* age: int (Required)
124+
* }
125+
* }</pre>
126+
*
127+
* @param input The input parameter.
128+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
129+
* @throws HttpResponseException thrown if the request is rejected by server.
130+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
131+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
132+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
133+
* @return the {@link Response} on successful completion of {@link Mono}.
134+
*/
135+
@Generated
136+
@ServiceMethod(returns = ReturnType.SINGLE)
137+
public Mono<Response<Void>> putRecursiveModelWithResponse(BinaryData input, RequestOptions requestOptions) {
138+
return this.serviceClient.putRecursiveModelWithResponseAsync(input, requestOptions);
139+
}
140+
141+
/**
142+
* The getMissingDiscriminator operation.
143+
* <p><strong>Response Body Schema</strong></p>
144+
*
145+
* <pre>{@code
146+
* {
147+
* kind: String(shark/salmon) (Required)
148+
* age: int (Required)
149+
* }
150+
* }</pre>
151+
*
152+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
153+
* @throws HttpResponseException thrown if the request is rejected by server.
154+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
155+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
156+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
157+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator along with
158+
* {@link Response} on successful completion of {@link Mono}.
159+
*/
160+
@Generated
161+
@ServiceMethod(returns = ReturnType.SINGLE)
162+
public Mono<Response<BinaryData>> getMissingDiscriminatorWithResponse(RequestOptions requestOptions) {
163+
return this.serviceClient.getMissingDiscriminatorWithResponseAsync(requestOptions);
164+
}
165+
166+
/**
167+
* The getWrongDiscriminator operation.
168+
* <p><strong>Response Body Schema</strong></p>
169+
*
170+
* <pre>{@code
171+
* {
172+
* kind: String(shark/salmon) (Required)
173+
* age: int (Required)
174+
* }
175+
* }</pre>
176+
*
177+
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
178+
* @throws HttpResponseException thrown if the request is rejected by server.
179+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
180+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
181+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
182+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator along with
183+
* {@link Response} on successful completion of {@link Mono}.
184+
*/
185+
@Generated
186+
@ServiceMethod(returns = ReturnType.SINGLE)
187+
public Mono<Response<BinaryData>> getWrongDiscriminatorWithResponse(RequestOptions requestOptions) {
188+
return this.serviceClient.getWrongDiscriminatorWithResponseAsync(requestOptions);
189+
}
190+
191+
/**
192+
* The getModel operation.
193+
*
194+
* @throws HttpResponseException thrown if the request is rejected by server.
195+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
196+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
197+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
198+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
199+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator on successful
200+
* completion of {@link Mono}.
201+
*/
202+
@Generated
203+
@ServiceMethod(returns = ReturnType.SINGLE)
204+
public Mono<Fish> getModel() {
205+
// Generated convenience method for getModelWithResponse
206+
RequestOptions requestOptions = new RequestOptions();
207+
return getModelWithResponse(requestOptions).flatMap(FluxUtil::toMono)
208+
.map(protocolMethodData -> protocolMethodData.toObject(Fish.class));
209+
}
210+
211+
/**
212+
* The putModel operation.
213+
*
214+
* @param input The input parameter.
215+
* @throws IllegalArgumentException thrown if parameters fail the validation.
216+
* @throws HttpResponseException thrown if the request is rejected by server.
217+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
218+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
219+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
220+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
221+
* @return A {@link Mono} that completes when a successful response is received.
222+
*/
223+
@Generated
224+
@ServiceMethod(returns = ReturnType.SINGLE)
225+
public Mono<Void> putModel(Fish input) {
226+
// Generated convenience method for putModelWithResponse
227+
RequestOptions requestOptions = new RequestOptions();
228+
return putModelWithResponse(BinaryData.fromObject(input), requestOptions).flatMap(FluxUtil::toMono);
229+
}
230+
231+
/**
232+
* The getRecursiveModel operation.
233+
*
234+
* @throws HttpResponseException thrown if the request is rejected by server.
235+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
236+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
237+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
238+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
239+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator on successful
240+
* completion of {@link Mono}.
241+
*/
242+
@Generated
243+
@ServiceMethod(returns = ReturnType.SINGLE)
244+
public Mono<Fish> getRecursiveModel() {
245+
// Generated convenience method for getRecursiveModelWithResponse
246+
RequestOptions requestOptions = new RequestOptions();
247+
return getRecursiveModelWithResponse(requestOptions).flatMap(FluxUtil::toMono)
248+
.map(protocolMethodData -> protocolMethodData.toObject(Fish.class));
249+
}
250+
251+
/**
252+
* The putRecursiveModel operation.
253+
*
254+
* @param input The input parameter.
255+
* @throws IllegalArgumentException thrown if parameters fail the validation.
256+
* @throws HttpResponseException thrown if the request is rejected by server.
257+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
258+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
259+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
260+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
261+
* @return A {@link Mono} that completes when a successful response is received.
262+
*/
263+
@Generated
264+
@ServiceMethod(returns = ReturnType.SINGLE)
265+
public Mono<Void> putRecursiveModel(Fish input) {
266+
// Generated convenience method for putRecursiveModelWithResponse
267+
RequestOptions requestOptions = new RequestOptions();
268+
return putRecursiveModelWithResponse(BinaryData.fromObject(input), requestOptions).flatMap(FluxUtil::toMono);
269+
}
270+
271+
/**
272+
* The getMissingDiscriminator operation.
273+
*
274+
* @throws HttpResponseException thrown if the request is rejected by server.
275+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
276+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
277+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
278+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
279+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator on successful
280+
* completion of {@link Mono}.
281+
*/
282+
@Generated
283+
@ServiceMethod(returns = ReturnType.SINGLE)
284+
public Mono<Fish> getMissingDiscriminator() {
285+
// Generated convenience method for getMissingDiscriminatorWithResponse
286+
RequestOptions requestOptions = new RequestOptions();
287+
return getMissingDiscriminatorWithResponse(requestOptions).flatMap(FluxUtil::toMono)
288+
.map(protocolMethodData -> protocolMethodData.toObject(Fish.class));
289+
}
290+
291+
/**
292+
* The getWrongDiscriminator operation.
293+
*
294+
* @throws HttpResponseException thrown if the request is rejected by server.
295+
* @throws ClientAuthenticationException thrown if the request is rejected by server on status code 401.
296+
* @throws ResourceNotFoundException thrown if the request is rejected by server on status code 404.
297+
* @throws ResourceModifiedException thrown if the request is rejected by server on status code 409.
298+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
299+
* @return this is base model for polymorphic multiple levels inheritance with a discriminator on successful
300+
* completion of {@link Mono}.
301+
*/
302+
@Generated
303+
@ServiceMethod(returns = ReturnType.SINGLE)
304+
public Mono<Fish> getWrongDiscriminator() {
305+
// Generated convenience method for getWrongDiscriminatorWithResponse
306+
RequestOptions requestOptions = new RequestOptions();
307+
return getWrongDiscriminatorWithResponse(requestOptions).flatMap(FluxUtil::toMono)
308+
.map(protocolMethodData -> protocolMethodData.toObject(Fish.class));
309+
}
310+
}

0 commit comments

Comments
 (0)