Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.
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 @@ -486,14 +486,16 @@ private static <T> void assertMethodExist(Class<T> clazz, String methodName, Str
@Test
public void testStreamStyleSerialization() throws IOException {
SerializerAdapter serializerAdapter = SerializerFactory.createDefaultManagementSerializerAdapter();
String pirCommunityGalleryResourceJson = "{\"name\":\"myName\",\"location\":\"myLocation\",\"type\":\"myType\"}";
String pirCommunityGalleryResourceJson = "{\"name\":\"myName\",\"location\":\"myLocation\",\"type\":\"myType\", \"identifier\": {\"uniqueId\": \"abc\"}}";

PirCommunityGalleryResource pirCommunityGalleryResource = serializerAdapter.deserialize(pirCommunityGalleryResourceJson, PirCommunityGalleryResource.class, SerializerEncoding.JSON);
Assertions.assertEquals("myName", pirCommunityGalleryResource.name());
Assertions.assertEquals("abc", pirCommunityGalleryResource.uniqueId());

String galleryJson = "{\"name\":\"myName\",\"location\":\"myLocation\",\"type\":\"myType\",\"disclaimer\":\"myDisclaimer\"}";
String galleryJson = "{\"name\":\"myName\",\"location\":\"myLocation\",\"type\":\"myType\", \"properties\": {\"disclaimer\":\"myDisclaimer\"}, \"identifier\": {\"uniqueId\": \"abc\"}}";
CommunityGalleryInner galleryInner = serializerAdapter.deserialize(galleryJson, CommunityGalleryInner.class, SerializerEncoding.JSON);
Assertions.assertEquals("myName", galleryInner.name());
Assertions.assertEquals("myDisclaimer", galleryInner.disclaimer());
Assertions.assertEquals("abc", pirCommunityGalleryResource.uniqueId());
}
}
31 changes: 29 additions & 2 deletions fluent-tests/swagger/stream-style-serialization.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,33 @@
"readOnly": true,
"type": "string",
"description": "Resource type"
},
"identifier": {
"x-ms-client-flatten": true,
"$ref": "#/definitions/CommunityGalleryIdentifier"
}
},
"description": "Base information about the community gallery resource in azure compute gallery."
},
"CommunityGalleryIdentifier": {
"properties": {
"uniqueId": {
"type": "string",
"description": "The unique id of this community gallery."
}
},
"description": "The identifier information of community gallery."
},
"CommunityGallery": {
"properties": {
"disclaimer": {
"name": {
"readOnly": true,
"type": "string",
"description": "Community gallery disclaimer"
"description": "Resource name in Child"
},
"properties": {
"x-ms-client-flatten": true,
"$ref": "#/definitions/CommunityGalleryProperties"
}
},
"allOf": [
Expand All @@ -66,6 +83,16 @@
}
],
"description": "Specifies information about the Community Gallery that you want to create or update."
},
"CommunityGalleryProperties": {
"type": "object",
"properties": {
"disclaimer": {
"type": "string",
"description": "Resource name"
}
},
"description": "Describes the properties of a community gallery."
}
},
"parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void init(){
/**
* Issue: https://github.com/Azure/autorest.java/issues/1320
* Remove duplicate setter methods from child schema when parent schema contains same property
* mainly to test {@link com.azure.autorest.template.ModelTemplate#getParentSettersToOverride(ClientModel, JavaSettings, List)}
* mainly to test {@link com.azure.autorest.template.ModelTemplate#getSuperSetters(ClientModel, JavaSettings, List)}
*/
@Test
public void deduplicateTest(){
Expand All @@ -47,7 +47,7 @@ public void deduplicateTest(){
propertyReferences.addAll(model.getPropertyReferences());
}
// real test here
List<ClientModelPropertyAccess> toOverride = templateAccessor.getParentSettersToOverride(model, settings, propertyReferences);
List<ClientModelPropertyAccess> toOverride = templateAccessor.getSuperSetters(model, settings, propertyReferences);
Assertions.assertEquals(toOverride.size(), 1);
}

Expand All @@ -57,8 +57,8 @@ public List<ClientModelPropertyReference> getClientModelPropertyReferences0(Clie
return super.getClientModelPropertyReferences(model);
}

public List<ClientModelPropertyAccess> getParentSettersToOverride(ClientModel model, JavaSettings settings, List<ClientModelPropertyReference> propertyReferences) {
return super.getParentSettersToOverride(model, settings, propertyReferences);
public List<ClientModelPropertyAccess> getSuperSetters(ClientModel model, JavaSettings settings, List<ClientModelPropertyReference> propertyReferences) {
return super.getSuperSetters(model, settings, propertyReferences);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public final void write(ClientModel model, JavaFile javaFile) {
TemplateUtil.addJsonGetter(classBlock, settings, property.getSerializedName());
}

boolean overridesParentGetter = isOverrideParentGetter(model, property, settings);
boolean overridesParentGetter = overridesParentGetter(model, property, settings);
if (overridesParentGetter) {
classBlock.annotation("Override");
}
Expand All @@ -185,7 +185,7 @@ public final void write(ClientModel model, JavaFile javaFile) {
methodBlock -> addGetterMethod(propertyWireType, propertyClientType, property, treatAsXml,
methodBlock, settings));

if (ClientModelUtil.hasSetter(property, settings) && !immutableModel) {
if (ClientModelUtil.needsPublicSetter(property, settings) && !immutableModel) {
generateSetterJavadoc(classBlock, model, property);
addGeneratedAnnotation(classBlock);
TemplateUtil.addJsonSetter(classBlock, settings, property.getSerializedName());
Expand Down Expand Up @@ -244,7 +244,7 @@ public final void write(ClientModel model, JavaFile javaFile) {

// add setters to override parent setters
if (!immutableModel) {
List<ClientModelPropertyAccess> settersToOverride = getParentSettersToOverride(model, settings,
List<ClientModelPropertyAccess> settersToOverride = getSuperSetters(model, settings,
propertyReferences);
for (ClientModelPropertyAccess parentProperty : settersToOverride) {
classBlock.javadocComment(JavaJavadocComment::inheritDoc);
Expand All @@ -267,7 +267,8 @@ public final void write(ClientModel model, JavaFile javaFile) {
if (settings.getClientFlattenAnnotationTarget() == JavaSettings.ClientFlattenAnnotationTarget.NONE) {
// reference to properties from flattened client model
for (ClientModelPropertyReference propertyReference : propertyReferences) {
if (!propertyReference.isFromFlattenedProperty()) {
propertyReference = getLocalFlattenedModelPropertyReference(propertyReference);
if (propertyReference == null) {
continue;
}

Expand Down Expand Up @@ -299,9 +300,10 @@ public final void write(ClientModel model, JavaFile javaFile) {
if (!propertyIsReadOnly) {
generateSetterJavadoc(classBlock, model, property);
addGeneratedAnnotation(classBlock);
ClientModelPropertyReference propertyReferenceFinal = propertyReference;
classBlock.publicMethod(String.format("%s %s(%s %s)", model.getName(), propertyReference.getSetterName(), propertyClientType, property.getName()), methodBlock -> {
methodBlock.ifBlock(String.format("this.%s() == null", targetProperty.getGetterName()), ifBlock ->
methodBlock.line(String.format("this.%s = new %s();", targetProperty.getName(), propertyReference.getTargetModelType())));
methodBlock.line(String.format("this.%s = new %s();", targetProperty.getName(), propertyReferenceFinal.getTargetModelType())));

methodBlock.line(String.format("this.%s().%s(%s);", targetProperty.getGetterName(), property.getSetterName(), property.getName()));
methodBlock.methodReturn("this");
Expand All @@ -322,14 +324,28 @@ public final void write(ClientModel model, JavaFile javaFile) {
});
}

/**
* Get the property reference referring to the local(field) flattened property.
*
* @param propertyReference propertyReference to check
* @return the property reference referring to the local(field) flattened property, null if it's not
*/
protected ClientModelPropertyReference getLocalFlattenedModelPropertyReference(ClientModelPropertyReference propertyReference) {
if (propertyReference.isFromFlattenedProperty()) {
return propertyReference;
}
// Not a flattening property, return null.
return null;
}

/**
* Whether the property's getter overrides parent getter.
* @param model the client model
* @param property the property to generate getter method
* @param settings {@link JavaSettings} instance
* @return whether the property's getter overrides parent getter
*/
protected boolean isOverrideParentGetter(ClientModel model, ClientModelProperty property, JavaSettings settings) {
protected boolean overridesParentGetter(ClientModel model, ClientModelProperty property, JavaSettings settings) {
// getter method of discriminator property in subclass is handled differently
return property.isPolymorphicDiscriminator() && !modelDefinesProperty(model, property);
}
Expand Down Expand Up @@ -412,21 +428,23 @@ protected void addSerializationImports(Set<String> imports, ClientModel model, J
}

/**
* Override parent setters if: 1. parent property has setter 2. child does not contain property that shadow this
* parent property, otherwise overridden parent setter methods will collide with child setter methods
* We generate super setters in child class if all of below conditions are met:
* 1. parent property has setter
* 2. child does not contain property that shadow this parent property, otherwise super setters
* will collide with child setters
*
* @see <a href="https://github.com/Azure/autorest.java/issues/1320">Issue 1320</a>
*/
protected List<ClientModelPropertyAccess> getParentSettersToOverride(ClientModel model, JavaSettings settings,
List<ClientModelPropertyReference> propertyReferences) {
Set<String> modelPropertyNames = model.getProperties().stream().map(ClientModelProperty::getName)
protected List<ClientModelPropertyAccess> getSuperSetters(ClientModel model, JavaSettings settings,
List<ClientModelPropertyReference> propertyReferences) {
Set<String> modelPropertyNames = getFieldProperties(model, settings).stream().map(ClientModelProperty::getName)
.collect(Collectors.toSet());
return propertyReferences.stream()
.filter(ClientModelPropertyReference::isFromParentModel)
.map(ClientModelPropertyReference::getReferenceProperty)
.filter(parentProperty -> {
// parent property doesn't have setter
if (!ClientModelUtil.hasSetter(parentProperty, settings)) {
if (!ClientModelUtil.needsPublicSetter(parentProperty, settings)) {
return false;
}
// child does not contain property that shadow this parent property
Expand Down Expand Up @@ -513,7 +531,7 @@ private void addFluentOrImmutableAnnotation(ClientModel model, boolean immutable
List<ClientModelPropertyReference> propertyReferences, JavaFile javaFile, JavaSettings settings) {
boolean fluent = !immutableOutputModel && Stream
.concat(model.getProperties().stream(), propertyReferences.stream())
.anyMatch(p -> ClientModelUtil.hasSetter(p, settings));
.anyMatch(p -> ClientModelUtil.needsPublicSetter(p, settings));

if (JavaSettings.getInstance().isBranded()) {
if (fluent) {
Expand Down
Loading