Fix 7457: [Ada] wrong order for generated structures in *-models.ads (#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
This commit is contained in:
Stephane Carrez 2018-01-22 06:27:39 +01:00 committed by William Cheng
parent 2b2b85eec7
commit 7d2b49085f
3 changed files with 222 additions and 209 deletions

View File

@ -459,6 +459,32 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
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.
@ -483,7 +509,8 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
}
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);
}
}
@ -495,14 +522,18 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
@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) {
@ -513,24 +544,6 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
// 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);
@ -542,7 +555,7 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
CodegenModel m = (CodegenModel) v;
LOGGER.info("Order: " + m.name);
}
}*/
} */
return postProcessModelsEnum(objs);
}

View File

@ -112,6 +112,62 @@ package body Samples.Petstore.Models is
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_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);
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
begin
Into.Start_Array (Name);
for Item of Value loop
Serialize (Into, "", Item);
end loop;
Into.End_Array (Name);
end Serialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
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, "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 Order_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Order_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
for Data of List loop
Deserialize (Data, "", Item);
Value.Append (Item);
end loop;
end Deserialize;
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Tag_Type) is
@ -160,6 +216,62 @@ package body Samples.Petstore.Models is
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_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.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
begin
Into.Start_Array (Name);
for Item of Value loop
Serialize (Into, "", Item);
end loop;
Into.End_Array (Name);
end Serialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_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);
end Deserialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Pet_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
for Data of List loop
Deserialize (Data, "", Item);
Value.Append (Item);
end loop;
end Deserialize;
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in User_Type) is
@ -219,116 +331,4 @@ package body Samples.Petstore.Models is
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_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);
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
begin
Into.Start_Array (Name);
for Item of Value loop
Serialize (Into, "", Item);
end loop;
Into.End_Array (Name);
end Serialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
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, "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 Order_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Order_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
for Data of List loop
Deserialize (Data, "", Item);
Value.Append (Item);
end loop;
end Deserialize;
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_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.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
begin
Into.Start_Array (Name);
for Item of Value loop
Serialize (Into, "", Item);
end loop;
Into.End_Array (Name);
end Serialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_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);
end Deserialize;
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type_Vectors.Vector) is
List : Swagger.Value_Array_Type;
Item : Pet_Type;
begin
Value.Clear;
Swagger.Streams.Deserialize (From, Name, List);
for Data of List loop
Deserialize (Data, "", Item);
Value.Append (Item);
end loop;
end Deserialize;
end Samples.Petstore.Models;

View File

@ -77,6 +77,42 @@ package Samples.Petstore.Models is
-- ------------------------------
-- Pet Order
-- An order for a pets from the pet store
-- ------------------------------
type Order_Type is
record
Id : Swagger.Nullable_Long;
Pet_Id : Swagger.Nullable_Long;
Quantity : Swagger.Nullable_Integer;
Ship_Date : Swagger.Nullable_Date;
Status : Swagger.Nullable_UString;
Complete : Swagger.Nullable_Boolean;
end record;
package Order_Type_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Order_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type_Vectors.Vector);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type_Vectors.Vector);
-- ------------------------------
-- Pet Tag
-- A tag for a pet
@ -109,6 +145,42 @@ package Samples.Petstore.Models is
-- ------------------------------
-- a Pet
-- A pet for sale in the pet store
-- ------------------------------
type Pet_Type is
record
Id : Swagger.Nullable_Long;
Category : Samples.Petstore.Models.Category_Type;
Name : Swagger.UString;
Photo_Urls : Swagger.Nullable_UString_Vectors.Vector;
Tags : Samples.Petstore.Models.Tag_Type_Vectors.Vector;
Status : Swagger.Nullable_UString;
end record;
package Pet_Type_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Pet_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type_Vectors.Vector);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type_Vectors.Vector);
-- ------------------------------
-- a User
-- A User who is purchasing from the pet store
@ -147,76 +219,4 @@ package Samples.Petstore.Models is
-- ------------------------------
-- Pet Order
-- An order for a pets from the pet store
-- ------------------------------
type Order_Type is
record
Id : Swagger.Nullable_Long;
Pet_Id : Swagger.Nullable_Long;
Quantity : Swagger.Nullable_Integer;
Ship_Date : Swagger.Nullable_Date;
Status : Swagger.Nullable_UString;
Complete : Swagger.Nullable_Boolean;
end record;
package Order_Type_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Order_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Order_Type_Vectors.Vector);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Order_Type_Vectors.Vector);
-- ------------------------------
-- a Pet
-- A pet for sale in the pet store
-- ------------------------------
type Pet_Type is
record
Id : Swagger.Nullable_Long;
Category : Samples.Petstore.Models.Category_Type;
Name : Swagger.UString;
Photo_Urls : Swagger.Nullable_UString_Vectors.Vector;
Tags : Samples.Petstore.Models.Tag_Type_Vectors.Vector;
Status : Swagger.Nullable_UString;
end record;
package Pet_Type_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Pet_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type);
procedure Serialize (Into : in out Swagger.Streams.Output_Stream'Class;
Name : in String;
Value : in Pet_Type_Vectors.Vector);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type);
procedure Deserialize (From : in Swagger.Value_Type;
Name : in String;
Value : out Pet_Type_Vectors.Vector);
end Samples.Petstore.Models;