Skip to content

Commit 65f93d6

Browse files
authored
Use xml name (#265)
* Update testserver * Bump to v2.0.19 * Fix generator and update testserver version * Accept that .XmlName sometimes throws for some reason * Update autorest * Fix polymorphism test * Downgrade autorest testserver version for now * Update package.json * Bump to v2.0.20. Fix testserver version range.
1 parent 1eb2909 commit 65f93d6

8 files changed

Lines changed: 203 additions & 15 deletions

File tree

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
.idea/
2+
target/
3+
*.iml
14
.gulp/
25
.vscode/
36
src/Model/
47
src/Properties/
8+
src/Templates/
59
test/
610
src/obj/
711
package/

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft.azure/autorest.java",
3-
"version": "2.0.18",
3+
"version": "2.0.20",
44
"description": "The Java extension for classic generators in AutoRest.",
55
"scripts": {
66
"autorest": "autorest",
@@ -32,8 +32,8 @@
3232
},
3333
"homepage": "https://github.com/Azure/autorest.java/blob/master/README.md",
3434
"devDependencies": {
35-
"@microsoft.azure/autorest.testserver": "2.3.14",
36-
"autorest": "2.0.4245",
35+
"@microsoft.azure/autorest.testserver": "2.5.12",
36+
"autorest": "^2.0.4280",
3737
"coffee-script": "1.12.7",
3838
"dotnet-sdk-2.0.0": "1.1.1",
3939
"gulp": "3.9.1",
@@ -53,4 +53,4 @@
5353
"dependencies": {
5454
"dotnet-2.0.0": "^1.1.0"
5555
}
56-
}
56+
}

src/JavaCodeGenerator.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,9 +1774,26 @@ private static ServiceModelProperty ParseModelProperty(AutoRestProperty autoRest
17741774
}
17751775
}
17761776

1777+
string xmlName;
1778+
if (autoRestProperty.ModelType is AutoRestCompositeType)
1779+
{
1780+
xmlName = autoRestProperty.ModelType.XmlName;
1781+
}
1782+
else
1783+
{
1784+
try
1785+
{
1786+
xmlName = autoRestProperty.XmlName;
1787+
}
1788+
catch
1789+
{
1790+
xmlName = null;
1791+
}
1792+
}
1793+
17771794
List<string> annotationArgumentList = new List<string>()
17781795
{
1779-
$"value = \"{(settings.ShouldGenerateXmlSerialization ? autoRestProperty.XmlName : autoRestProperty.SerializedName)}\""
1796+
$"value = \"{(settings.ShouldGenerateXmlSerialization ? xmlName : autoRestProperty.SerializedName)}\""
17801797
};
17811798
if (autoRestProperty.IsRequired)
17821799
{
@@ -1790,16 +1807,6 @@ private static ServiceModelProperty ParseModelProperty(AutoRestProperty autoRest
17901807

17911808
bool isXmlAttribute = autoRestProperty.XmlIsAttribute;
17921809

1793-
string xmlName;
1794-
try
1795-
{
1796-
xmlName = autoRestProperty.XmlName;
1797-
}
1798-
catch (ArgumentNullException)
1799-
{
1800-
xmlName = null;
1801-
}
1802-
18031810
string serializedName = autoRestProperty.SerializedName;
18041811

18051812
bool isXmlWrapper = autoRestProperty.XmlIsWrapped;

test/vanilla/src/main/java/fixtures/bodycomplex/Polymorphisms.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,45 @@ public interface Polymorphisms {
295295
*/
296296
Completable putComplicatedAsync(@NonNull Salmon complexBody);
297297

298+
/**
299+
* Put complex types that are polymorphic, omitting the discriminator.
300+
*
301+
* @param complexBody the Salmon value.
302+
* @throws IllegalArgumentException thrown if parameters fail the validation.
303+
* @throws ErrorException thrown if the request is rejected by server.
304+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
305+
* @return the Salmon object if successful.
306+
*/
307+
Salmon putMissingDiscriminator(@NonNull Salmon complexBody);
308+
309+
/**
310+
* Put complex types that are polymorphic, omitting the discriminator.
311+
*
312+
* @param complexBody the Salmon value.
313+
* @param serviceCallback the async ServiceCallback to handle successful and failed responses.
314+
* @throws IllegalArgumentException thrown if parameters fail the validation.
315+
* @return a ServiceFuture which will be completed with the result of the network request.
316+
*/
317+
ServiceFuture<Salmon> putMissingDiscriminatorAsync(@NonNull Salmon complexBody, ServiceCallback<Salmon> serviceCallback);
318+
319+
/**
320+
* Put complex types that are polymorphic, omitting the discriminator.
321+
*
322+
* @param complexBody the Salmon value.
323+
* @throws IllegalArgumentException thrown if parameters fail the validation.
324+
* @return a Single which performs the network request upon subscription.
325+
*/
326+
Single<BodyResponse<Salmon>> putMissingDiscriminatorWithRestResponseAsync(@NonNull Salmon complexBody);
327+
328+
/**
329+
* Put complex types that are polymorphic, omitting the discriminator.
330+
*
331+
* @param complexBody the Salmon value.
332+
* @throws IllegalArgumentException thrown if parameters fail the validation.
333+
* @return a Single which performs the network request upon subscription.
334+
*/
335+
Maybe<Salmon> putMissingDiscriminatorAsync(@NonNull Salmon complexBody);
336+
298337
/**
299338
* Put complex types that are polymorphic, attempting to omit required 'birthday' field - the request should not be allowed from the client.
300339
*

test/vanilla/src/main/java/fixtures/bodycomplex/implementation/PolymorphismsImpl.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ private interface PolymorphismsService {
8282
@UnexpectedResponseExceptionType(ErrorException.class)
8383
Single<VoidResponse> putComplicated(@BodyParam("application/json; charset=utf-8") Salmon complexBody);
8484

85+
@PUT("complex/polymorphism/missingdiscriminator")
86+
@ExpectedResponses({200})
87+
@UnexpectedResponseExceptionType(ErrorException.class)
88+
Single<BodyResponse<Salmon>> putMissingDiscriminator(@BodyParam("application/json; charset=utf-8") Salmon complexBody);
89+
8590
@PUT("complex/polymorphism/missingrequired/invalid")
8691
@ExpectedResponses({200})
8792
@UnexpectedResponseExceptionType(ErrorException.class)
@@ -400,6 +405,58 @@ public Completable putComplicatedAsync(@NonNull Salmon complexBody) {
400405
.toCompletable();
401406
}
402407

408+
/**
409+
* Put complex types that are polymorphic, omitting the discriminator.
410+
*
411+
* @param complexBody the Salmon value.
412+
* @throws IllegalArgumentException thrown if parameters fail the validation.
413+
* @throws ErrorException thrown if the request is rejected by server.
414+
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
415+
* @return the Salmon object if successful.
416+
*/
417+
public Salmon putMissingDiscriminator(@NonNull Salmon complexBody) {
418+
return putMissingDiscriminatorAsync(complexBody).blockingGet();
419+
}
420+
421+
/**
422+
* Put complex types that are polymorphic, omitting the discriminator.
423+
*
424+
* @param complexBody the Salmon value.
425+
* @param serviceCallback the async ServiceCallback to handle successful and failed responses.
426+
* @throws IllegalArgumentException thrown if parameters fail the validation.
427+
* @return a ServiceFuture which will be completed with the result of the network request.
428+
*/
429+
public ServiceFuture<Salmon> putMissingDiscriminatorAsync(@NonNull Salmon complexBody, ServiceCallback<Salmon> serviceCallback) {
430+
return ServiceFuture.fromBody(putMissingDiscriminatorAsync(complexBody), serviceCallback);
431+
}
432+
433+
/**
434+
* Put complex types that are polymorphic, omitting the discriminator.
435+
*
436+
* @param complexBody the Salmon value.
437+
* @throws IllegalArgumentException thrown if parameters fail the validation.
438+
* @return a Single which performs the network request upon subscription.
439+
*/
440+
public Single<BodyResponse<Salmon>> putMissingDiscriminatorWithRestResponseAsync(@NonNull Salmon complexBody) {
441+
if (complexBody == null) {
442+
throw new IllegalArgumentException("Parameter complexBody is required and cannot be null.");
443+
}
444+
Validator.validate(complexBody);
445+
return service.putMissingDiscriminator(complexBody);
446+
}
447+
448+
/**
449+
* Put complex types that are polymorphic, omitting the discriminator.
450+
*
451+
* @param complexBody the Salmon value.
452+
* @throws IllegalArgumentException thrown if parameters fail the validation.
453+
* @return a Single which performs the network request upon subscription.
454+
*/
455+
public Maybe<Salmon> putMissingDiscriminatorAsync(@NonNull Salmon complexBody) {
456+
return putMissingDiscriminatorWithRestResponseAsync(complexBody)
457+
.flatMapMaybe((BodyResponse<Salmon> res) -> res.body() == null ? Maybe.empty() : Maybe.just(res.body()));
458+
}
459+
403460
/**
404461
* Put complex types that are polymorphic, attempting to omit required 'birthday' field - the request should not be allowed from the client.
405462
*
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*
6+
* Code generated by Microsoft (R) AutoRest Code Generator.
7+
* Changes may cause incorrect behavior and will be lost if the code is
8+
* regenerated.
9+
*/
10+
11+
package fixtures.bodycomplex.models;
12+
13+
import com.fasterxml.jackson.annotation.JsonCreator;
14+
import com.microsoft.rest.v2.ExpandableStringEnum;
15+
import java.util.Collection;
16+
17+
/**
18+
* Defines values for GoblinSharkColor.
19+
*/
20+
public final class GoblinSharkColor extends ExpandableStringEnum<GoblinSharkColor> {
21+
/**
22+
* Static value pink for GoblinSharkColor.
23+
*/
24+
public static final GoblinSharkColor PINK = fromString("pink");
25+
26+
/**
27+
* Static value gray for GoblinSharkColor.
28+
*/
29+
public static final GoblinSharkColor GRAY = fromString("gray");
30+
31+
/**
32+
* Static value brown for GoblinSharkColor.
33+
*/
34+
public static final GoblinSharkColor BROWN = fromString("brown");
35+
36+
/**
37+
* Creates or finds a GoblinSharkColor from its string representation.
38+
*
39+
* @param name a name to look for.
40+
* @return the corresponding GoblinSharkColor.
41+
*/
42+
@JsonCreator
43+
public static GoblinSharkColor fromString(String name) {
44+
return fromString(name, GoblinSharkColor.class);
45+
}
46+
47+
/**
48+
* @return known GoblinSharkColor values.
49+
*/
50+
public static Collection<GoblinSharkColor> values() {
51+
return values(GoblinSharkColor.class);
52+
}
53+
}

test/vanilla/src/main/java/fixtures/bodycomplex/models/Goblinshark.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public final class Goblinshark extends Shark {
2626
@JsonProperty(value = "jawsize")
2727
private Integer jawsize;
2828

29+
/**
30+
* Colors possible. Possible values include: 'pink', 'gray', 'brown'.
31+
*/
32+
@JsonProperty(value = "color")
33+
private GoblinSharkColor color;
34+
2935
/**
3036
* Get the jawsize value.
3137
*
@@ -45,4 +51,24 @@ public Goblinshark withJawsize(Integer jawsize) {
4551
this.jawsize = jawsize;
4652
return this;
4753
}
54+
55+
/**
56+
* Get the color value.
57+
*
58+
* @return the color value.
59+
*/
60+
public GoblinSharkColor color() {
61+
return this.color;
62+
}
63+
64+
/**
65+
* Set the color value.
66+
*
67+
* @param color the color value to set.
68+
* @return the Goblinshark object itself.
69+
*/
70+
public Goblinshark withColor(GoblinSharkColor color) {
71+
this.color = color;
72+
return this;
73+
}
4874
}

test/vanilla/src/test/java/fixtures/bodycomplex/PolymorphismTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fixtures.bodycomplex.implementation.AutoRestComplexTestServiceImpl;
44
import fixtures.bodycomplex.models.Fish;
5+
import fixtures.bodycomplex.models.GoblinSharkColor;
56
import fixtures.bodycomplex.models.Goblinshark;
67
import fixtures.bodycomplex.models.Salmon;
78
import fixtures.bodycomplex.models.Sawshark;
@@ -73,6 +74,7 @@ public void putValid() {
7374
sib3.withLength(30.0);
7475
sib3.withSpecies("scary");
7576
sib3.withJawsize(5);
77+
sib3.withColor(GoblinSharkColor.fromString("pinkish-gray"));
7678
body.siblings().add(sib3);
7779

7880
client.polymorphisms().putValid(body);

0 commit comments

Comments
 (0)