Skip to content

Commit

Permalink
Fix 7457: [Ada] wrong order for generated structures in *-models.ads (#…
Browse files Browse the repository at this point in the history
…7462)

* Fix the comparison between two model types to compare them recursively
and make sure we sort the model types according to their dependencies

* Run bin/ada-petstore.sh to update the generated Ada client samples
  • Loading branch information
stcarrez authored and wing328 committed Jan 22, 2018
1 parent 2b2b85e commit 7d2b490
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,32 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
return objs;
}

/**
* Check if type1 depends on type2 recursively.
*
* @param type1 first model type name
* @param type2 second model type name
* @return true if type1 depends on type2
*/
private Boolean dependsOn(String type1, String type2) {
final String fullTypeName = modelPackage + ".Models." + type2;
if (type1.equals(type2) || type1.equals(fullTypeName)) {
return true;
}
final List<String> lhsList = modelDepends.get(type1);
if (lhsList != null) {
for (final String S : lhsList) {
if (S.equals(type2) || S.equals(fullTypeName)) {
return true;
}
if (dependsOn(S, type2)) {
return true;
}
}
}
return false;
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// Collect the model dependencies.
Expand All @@ -483,7 +509,8 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}
p.vendorExtensions.put("x-is-model-type", isModel);
}
modelDepends.put(m.name, d);
modelDepends.put(m.classname, d);
modelDepends.put(modelPackage + ".Models." + m.classname, d);
orderedModels.add(model);
}
}
Expand All @@ -495,14 +522,18 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
@Override
public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
Object v = lhs.get("model");
String lhsName = ((CodegenModel) v).name;
String lhsName = ((CodegenModel) v).classname;
v = rhs.get("model");
String rhsName = ((CodegenModel) v).name;
String rhsName = ((CodegenModel) v).classname;
List<String> lhsList = deps.get(lhsName);
List<String> rhsList = deps.get(rhsName);
if (lhsList == rhsList) {
// LOGGER.info("First compare " + lhsName + "<" + rhsName);
return lhsName.compareTo(rhsName);
if (dependsOn(lhsName, rhsName)) {
// LOGGER.info("Type " + lhsName + " depends on " + rhsName);
return 1;
}
if (dependsOn(rhsName, lhsName)) {
// LOGGER.info("Type " + rhsName + " depends on " + lhsName);
return -1;
}
// Put models without dependencies first.
if (lhsList == null) {
Expand All @@ -513,24 +544,6 @@ public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
// LOGGER.info(" No check " + lhsName + ", empty " + rhsName);
return 1;
}
// Put models that depend on another after.
if (lhsList.contains(rhsName)) {
// LOGGER.info(" LSH " + lhsName + " uses " + rhsName);
return 1;
}
if (rhsList.contains(lhsName)) {
// LOGGER.info(" RHS " + rhsName + " uses " + lhsName);
return -1;
}
// Put models with less dependencies first.
if (lhsList.size() < rhsList.size()) {
// LOGGER.info(" LSH size " + lhsName + " < RHS size " + rhsName);
return -1;
}
if (lhsList.size() > rhsList.size()) {
// LOGGER.info(" LSH size " + lhsName + " > RHS size " + rhsName);
return 1;
}
// Sort models on their name.
// LOGGER.info("Compare " + lhsName + "<" + rhsName);
return lhsName.compareTo(rhsName);
Expand All @@ -542,7 +555,7 @@ public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
CodegenModel m = (CodegenModel) v;
LOGGER.info("Order: " + m.name);
}
}*/
} */
return postProcessModelsEnum(objs);
}

Expand Down
108 changes: 54 additions & 54 deletions samples/client/petstore/ada/src/model/samples-petstore-models.adb
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ package body Samples.Petstore.Models is

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Tag_Type) is
Value : in Order_Type) is
begin
Into.Start_Entity (Name);
Serialize (Into, "id", Value.Id);
Into.Write_Entity ("name", Value.Name);
Serialize (Into, "petId", Value.Pet_Id);
Into.Write_Entity ("quantity", Value.Quantity);
Into.Write_Entity ("shipDate", Value.Ship_Date);
Into.Write_Entity ("status", Value.Status);
Into.Write_Entity ("complete", Value.Complete);
Into.End_Entity (Name);
end Serialize;

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Tag_Type_Vectors.Vector) is
Value : in Order_Type_Vectors.Vector) is
begin
Into.Start_Array (Name);
for Item of Value loop
Expand All @@ -135,19 +139,23 @@ package body Samples.Petstore.Models is

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Tag_Type) is
Value : out Order_Type) is
Object : Swagger.Value_Type;
begin
Swagger.Streams.Deserialize (From, Name, Object);
Swagger.Streams.Deserialize (Object, "id", Value.Id);
Swagger.Streams.Deserialize (Object, "name", Value.Name);
Swagger.Streams.Deserialize (Object, "petId", Value.Pet_Id);
Swagger.Streams.Deserialize (Object, "quantity", Value.Quantity);
Deserialize (Object, "shipDate", Value.Ship_Date);
Swagger.Streams.Deserialize (Object, "status", Value.Status);
Swagger.Streams.Deserialize (Object, "complete", Value.Complete);
end Deserialize;

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Tag_Type_Vectors.Vector) is
Value : out Order_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Tag_Type;
Item : Order_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
Expand All @@ -162,23 +170,17 @@ package body Samples.Petstore.Models is

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in User_Type) is
Value : in Tag_Type) is
begin
Into.Start_Entity (Name);
Serialize (Into, "id", Value.Id);
Into.Write_Entity ("username", Value.Username);
Into.Write_Entity ("firstName", Value.First_Name);
Into.Write_Entity ("lastName", Value.Last_Name);
Into.Write_Entity ("email", Value.Email);
Into.Write_Entity ("password", Value.Password);
Into.Write_Entity ("phone", Value.Phone);
Into.Write_Entity ("userStatus", Value.User_Status);
Into.Write_Entity ("name", Value.Name);
Into.End_Entity (Name);
end Serialize;

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in User_Type_Vectors.Vector) is
Value : in Tag_Type_Vectors.Vector) is
begin
Into.Start_Array (Name);
for Item of Value loop
Expand All @@ -189,25 +191,19 @@ package body Samples.Petstore.Models is

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out User_Type) is
Value : out Tag_Type) is
Object : Swagger.Value_Type;
begin
Swagger.Streams.Deserialize (From, Name, Object);
Swagger.Streams.Deserialize (Object, "id", Value.Id);
Swagger.Streams.Deserialize (Object, "username", Value.Username);
Swagger.Streams.Deserialize (Object, "firstName", Value.First_Name);
Swagger.Streams.Deserialize (Object, "lastName", Value.Last_Name);
Swagger.Streams.Deserialize (Object, "email", Value.Email);
Swagger.Streams.Deserialize (Object, "password", Value.Password);
Swagger.Streams.Deserialize (Object, "phone", Value.Phone);
Swagger.Streams.Deserialize (Object, "userStatus", Value.User_Status);
Swagger.Streams.Deserialize (Object, "name", Value.Name);
end Deserialize;

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out User_Type_Vectors.Vector) is
Value : out Tag_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : User_Type;
Item : Tag_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
Expand All @@ -222,21 +218,21 @@ package body Samples.Petstore.Models is

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type) is
Value : in Pet_Type) is
begin
Into.Start_Entity (Name);
Serialize (Into, "id", Value.Id);
Serialize (Into, "petId", Value.Pet_Id);
Into.Write_Entity ("quantity", Value.Quantity);
Into.Write_Entity ("shipDate", Value.Ship_Date);
Serialize (Into, "category", Value.Category);
Into.Write_Entity ("name", Value.Name);
Serialize (Into, "photoUrls", Value.Photo_Urls);
Serialize (Into, "tags", Value.Tags);
Into.Write_Entity ("status", Value.Status);
Into.Write_Entity ("complete", Value.Complete);
Into.End_Entity (Name);
end Serialize;

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type_Vectors.Vector) is
Value : in Pet_Type_Vectors.Vector) is
begin
Into.Start_Array (Name);
for Item of Value loop
Expand All @@ -247,23 +243,23 @@ package body Samples.Petstore.Models is

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type) is
Value : out Pet_Type) is
Object : Swagger.Value_Type;
begin
Swagger.Streams.Deserialize (From, Name, Object);
Swagger.Streams.Deserialize (Object, "id", Value.Id);
Swagger.Streams.Deserialize (Object, "petId", Value.Pet_Id);
Swagger.Streams.Deserialize (Object, "quantity", Value.Quantity);
Deserialize (Object, "shipDate", Value.Ship_Date);
Deserialize (Object, "category", Value.Category);
Swagger.Streams.Deserialize (Object, "name", Value.Name);
Swagger.Streams.Deserialize (Object, "photoUrls", Value.Photo_Urls);
Deserialize (Object, "tags", Value.Tags);
Swagger.Streams.Deserialize (Object, "status", Value.Status);
Swagger.Streams.Deserialize (Object, "complete", Value.Complete);
end Deserialize;

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type_Vectors.Vector) is
Value : out Pet_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Order_Type;
Item : Pet_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
Expand All @@ -278,21 +274,23 @@ package body Samples.Petstore.Models is

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type) is
Value : in User_Type) is
begin
Into.Start_Entity (Name);
Serialize (Into, "id", Value.Id);
Serialize (Into, "category", Value.Category);
Into.Write_Entity ("name", Value.Name);
Serialize (Into, "photoUrls", Value.Photo_Urls);
Serialize (Into, "tags", Value.Tags);
Into.Write_Entity ("status", Value.Status);
Into.Write_Entity ("username", Value.Username);
Into.Write_Entity ("firstName", Value.First_Name);
Into.Write_Entity ("lastName", Value.Last_Name);
Into.Write_Entity ("email", Value.Email);
Into.Write_Entity ("password", Value.Password);
Into.Write_Entity ("phone", Value.Phone);
Into.Write_Entity ("userStatus", Value.User_Status);
Into.End_Entity (Name);
end Serialize;

procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type_Vectors.Vector) is
Value : in User_Type_Vectors.Vector) is
begin
Into.Start_Array (Name);
for Item of Value loop
Expand All @@ -303,23 +301,25 @@ package body Samples.Petstore.Models is

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type) is
Value : out User_Type) is
Object : Swagger.Value_Type;
begin
Swagger.Streams.Deserialize (From, Name, Object);
Swagger.Streams.Deserialize (Object, "id", Value.Id);
Deserialize (Object, "category", Value.Category);
Swagger.Streams.Deserialize (Object, "name", Value.Name);
Swagger.Streams.Deserialize (Object, "photoUrls", Value.Photo_Urls);
Deserialize (Object, "tags", Value.Tags);
Swagger.Streams.Deserialize (Object, "status", Value.Status);
Swagger.Streams.Deserialize (Object, "username", Value.Username);
Swagger.Streams.Deserialize (Object, "firstName", Value.First_Name);
Swagger.Streams.Deserialize (Object, "lastName", Value.Last_Name);
Swagger.Streams.Deserialize (Object, "email", Value.Email);
Swagger.Streams.Deserialize (Object, "password", Value.Password);
Swagger.Streams.Deserialize (Object, "phone", Value.Phone);
Swagger.Streams.Deserialize (Object, "userStatus", Value.User_Status);
end Deserialize;

procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type_Vectors.Vector) is
Value : out User_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Pet_Type;
Item : User_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
Expand Down
Loading

0 comments on commit 7d2b490

Please sign in to comment.