Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {

Map<String, ClientModelPropertyWithMetadata> flattenedProperties = new LinkedHashMap<>();
boolean hasRequiredProperties = false;
boolean hasConstructorArguments = false;
superConstructorProperties = new LinkedHashMap<>();
superRequiredProperties = new LinkedHashMap<>();
superSetterProperties = new LinkedHashMap<>();
Expand Down Expand Up @@ -140,6 +141,7 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {
superPropertyConsumer(property, superRequiredProperties, superConstructorProperties,
superReadOnlyProperties, superSetterProperties, settings);
hasRequiredProperties |= property.isRequired();
hasConstructorArguments |= ClientModelUtil.includePropertyInConstructor(property, settings);
}

if (property.getNeedsFlatten()) {
Expand Down Expand Up @@ -181,6 +183,7 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {
if (!property.isConstant()) {
if (ClientModelUtil.includePropertyInConstructor(property, settings)) {
constructorProperties.put(property.getSerializedName(), property);
hasConstructorArguments = true;
} else {
readOnlyProperties.put(property.getSerializedName(), property);
}
Expand Down Expand Up @@ -227,16 +230,11 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {
}
}

boolean requiredConstructorProperties = hasRequiredProperties && settings.isRequiredFieldsAsConstructorArgs();
boolean readOnlyConstructorProperties = settings.isRequiredFieldsAsConstructorArgs()
&& settings.isIncludeReadOnlyInConstructorArgs()
&& (!CoreUtils.isNullOrEmpty(readOnlyProperties) || !CoreUtils.isNullOrEmpty(superReadOnlyProperties));

this.hasRequiredProperties = hasRequiredProperties;
this.requiredPropertiesCount = requiredProperties.size() + superRequiredProperties.size();
this.setterPropertiesCount = setterProperties.size() + superSetterProperties.size();
this.readOnlyPropertiesCount = readOnlyProperties.size() + superReadOnlyProperties.size();
this.hasConstructorArguments = requiredConstructorProperties || readOnlyConstructorProperties;
this.hasConstructorArguments = hasConstructorArguments;
this.hasXmlElements = hasXmlElements;
this.hasXmlTexts = hasXmlTexts;
this.discriminatorProperty = discriminatorProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,64 @@ public class ClientModel {
* The package that this model class belongs to.
*/
private final String packageName;

/**
* Get the name of this model.
*/
private final String name;

private final String fullName;

/**
* Get the imports for this model.
*/
private final List<String> imports;

/**
* Get the description of this model.
*/
private final String description;

/**
* Get whether this model is part of a polymorphic hierarchy.
*/
private final boolean isPolymorphic;

/**
* Get whether this model is a parent in a polymorphic hierarchy.
*/
private final boolean isPolymorphicParent;

/**
* Get the property that determines which polymorphic model type to create.
*/
private final ClientModelProperty polymorphicDiscriminator;

/**
* Get the name of the property that determines which polymorphic model type to create.
*/
private final String polymorphicDiscriminatorName;

/**
* Get the name that is used for this model when it is serialized.
*/
private final String serializedName;

/**
* Get whether this model needs serialization flattening.
*/
private final boolean needsFlatten;

/**
* Get the parent model of this model.
*/
private final String parentModelName;

/**
* Get the models that derive from this model.
*/
private final List<ClientModel> derivedModels;

/**
* Get the name that will be used for this model's XML element representation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -301,7 +301,7 @@ public final void addImportsTo(Set<String> imports, boolean shouldGenerateXmlSer
imports.add(JsonIgnore.class.getName());
imports.add(JsonAnySetter.class.getName());
imports.add(JsonAnyGetter.class.getName());
imports.add(HashMap.class.getName());
imports.add(LinkedHashMap.class.getName());
}

if (settings.getClientFlattenAnnotationTarget() == JavaSettings.ClientFlattenAnnotationTarget.FIELD && needsFlatten) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public class ClientModelPropertyReference implements ClientModelPropertyAccess {
private final ClientModel targetModel;
private final ClientModelProperty targetProperty;

private ClientModelPropertyReference(ClientModelProperty targetProperty,
ClientModel targetModel,
ClientModelPropertyAccess referenceProperty,
String name) {
private ClientModelPropertyReference(ClientModelProperty targetProperty, ClientModel targetModel,
ClientModelPropertyAccess referenceProperty, String name) {
this.targetProperty = targetProperty;
this.targetModel = targetModel;
this.referenceProperty = referenceProperty;
Expand All @@ -46,16 +44,12 @@ public static ClientModelPropertyReference ofParentProperty(ClientModelPropertyR
}

public static ClientModelPropertyReference ofFlattenProperty(ClientModelProperty targetProperty,
ClientModel targetModel,
ClientModelProperty property,
String name) {
ClientModel targetModel, ClientModelProperty property, String name) {
return new ClientModelPropertyReference(targetProperty, targetModel, property, name);
}

public static ClientModelPropertyReference ofFlattenProperty(ClientModelProperty targetProperty,
ClientModel targetModel,
ClientModelPropertyReference referenceProperty,
String name) {
ClientModel targetModel, ClientModelPropertyReference referenceProperty, String name) {
if (!referenceProperty.isFromFlattenedProperty()) {
throw new IllegalArgumentException("Property is not from flattened model: " + referenceProperty.getName());
}
Expand Down Expand Up @@ -88,7 +82,8 @@ public List<ClientModelProperty> getAllProperties() {
} else if (referenceProperty instanceof ClientModelPropertyReference) {
properties.addAll(((ClientModelPropertyReference) referenceProperty).getAllProperties());
} else {
throw new IllegalStateException("Unknown subclass of ClientModelPropertyAccess: " + referenceProperty.getClass().getName());
throw new IllegalStateException(
"Unknown subclass of ClientModelPropertyAccess: " + referenceProperty.getClass().getName());
}
return properties;
}
Expand All @@ -109,7 +104,8 @@ public String getDescription() {

@Override
public String getGetterName() {
return CodeNamer.getModelNamer().modelPropertyGetterName(this.referenceProperty.getClientType(), this.getName());
return CodeNamer.getModelNamer()
.modelPropertyGetterName(this.referenceProperty.getClientType(), this.getName());
}

@Override
Expand Down Expand Up @@ -149,7 +145,8 @@ public boolean isRequired() {

@Override
public boolean isRequiredForCreate() {
return (targetProperty == null || targetProperty.isRequiredForCreate()) && referenceProperty.isRequiredForCreate();
return (targetProperty == null || targetProperty.isRequiredForCreate())
&& referenceProperty.isRequiredForCreate();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public final void protectedMemberVariable(String variableType, String variableNa
addNewLine = true;
}

/**
* Adds a variable with the given declaration and visibility.
* <p>
* This doesn't support modifiers. If you need to add modifiers, use
* {@link #variable(String, JavaVisibility, JavaModifier...)}. This will just be a non-final, non-static variable
* with the given visibility.
*
* @param variableDeclaration The variable declaration.
* @param visibility The visibility of the variable.
*/
public final void variable(String variableDeclaration, JavaVisibility visibility) {
addExpectedNewLine();
contents.line(visibility + " " + variableDeclaration + ";");
addNewLine = true;
}

/**
* Adds a variable with the given declaration, visibility, and modifiers.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,13 @@ private static List<MethodParameter> getParameters(ClientMethod method, boolean
* @return the name of the variable that holds the converted parameter
*/
private static String writeParameterConversionExpressionWithJsonMergePatchEnabled(JavaBlock javaBlock, String convenientParameterTypeName, String convenientParameterName, String expression) {
String variableName = convenientParameterName + "InBinaryData";
javaBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, true);", convenientParameterTypeName, convenientParameterName));
javaBlock.line(String.format("BinaryData %1$s = BinaryData.fromBytes(%2$s.toBytes());", variableName, expression)); // BinaryData.fromObject() will not fire serialization, use toBytes() to fire serialization
javaBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, false);", convenientParameterTypeName, convenientParameterName));
return variableName;
String variableName = convenientParameterName + "InBinaryData";
javaBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, true);", convenientParameterTypeName, convenientParameterName));
javaBlock.line("BinaryData " + variableName + " = " + expression + ";");
javaBlock.line("// BinaryData.fromObject() will not fire serialization, use getLength() to fire serialization.");
javaBlock.line(variableName + ".getLength();");
javaBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, false);", convenientParameterTypeName, convenientParameterName));
return variableName;
}

protected static class MethodParameter {
Expand Down
Loading