|
1 | 1 | package io.swagger.codegen.languages.java;
|
2 | 2 |
|
3 | 3 | import com.google.common.collect.Sets;
|
| 4 | + |
4 | 5 | import io.swagger.codegen.ClientOptInput;
|
5 | 6 | import io.swagger.codegen.CodegenConstants;
|
6 | 7 | import io.swagger.codegen.CodegenModel;
|
| 8 | +import io.swagger.codegen.CodegenOperation; |
7 | 9 | import io.swagger.codegen.CodegenParameter;
|
8 | 10 | import io.swagger.codegen.CodegenProperty;
|
| 11 | +import io.swagger.codegen.CodegenResponse; |
9 | 12 | import io.swagger.codegen.DefaultGenerator;
|
10 | 13 | import io.swagger.codegen.config.CodegenConfigurator;
|
11 | 14 | import io.swagger.codegen.languages.DefaultCodegenConfig;
|
| 15 | +import io.swagger.v3.oas.models.Operation; |
12 | 16 | import io.swagger.v3.oas.models.media.ArraySchema;
|
13 | 17 | import io.swagger.v3.oas.models.media.BooleanSchema;
|
14 | 18 | import io.swagger.v3.oas.models.media.ByteArraySchema;
|
| 19 | +import io.swagger.v3.oas.models.media.Content; |
15 | 20 | import io.swagger.v3.oas.models.media.DateTimeSchema;
|
16 | 21 | import io.swagger.v3.oas.models.media.IntegerSchema;
|
17 | 22 | import io.swagger.v3.oas.models.media.MapSchema;
|
| 23 | +import io.swagger.v3.oas.models.media.MediaType; |
18 | 24 | import io.swagger.v3.oas.models.media.NumberSchema;
|
19 | 25 | import io.swagger.v3.oas.models.media.ObjectSchema;
|
20 | 26 | import io.swagger.v3.oas.models.media.Schema;
|
21 | 27 | import io.swagger.v3.oas.models.media.StringSchema;
|
22 | 28 | import io.swagger.v3.oas.models.media.XML;
|
23 | 29 | import io.swagger.v3.oas.models.parameters.Parameter;
|
24 | 30 | import io.swagger.v3.oas.models.parameters.QueryParameter;
|
| 31 | +import io.swagger.v3.oas.models.parameters.RequestBody; |
| 32 | +import io.swagger.v3.oas.models.responses.ApiResponse; |
| 33 | +import io.swagger.v3.oas.models.responses.ApiResponses; |
25 | 34 | import io.swagger.v3.parser.util.SchemaTypeUtil;
|
| 35 | + |
26 | 36 | import org.junit.rules.TemporaryFolder;
|
27 | 37 | import org.testng.Assert;
|
28 | 38 | import org.testng.annotations.DataProvider;
|
@@ -825,6 +835,151 @@ public void integerPropertyInReferencedSchemaTest() {
|
825 | 835 | Assert.assertEquals(cp2.getter, "getInteger2");
|
826 | 836 | }
|
827 | 837 |
|
| 838 | + @Test(description = "convert an array schema") |
| 839 | + public void arraySchemaTest() { |
| 840 | + final Schema testSchema = new ObjectSchema() |
| 841 | + .addProperties("pets", new ArraySchema() |
| 842 | + .items(new Schema<>().$ref("#/components/schemas/Pet"))); |
| 843 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 844 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 845 | + final CodegenModel cm = codegen.fromModel("test", testSchema, allDefinitions); |
| 846 | + |
| 847 | + Assert.assertEquals(cm.vars.size(), 1); |
| 848 | + CodegenProperty cp1 = cm.vars.get(0); |
| 849 | + Assert.assertEquals(cp1.baseName, "pets"); |
| 850 | + Assert.assertEquals(cp1.datatype, "List<Pet>"); |
| 851 | + Assert.assertEquals(cp1.name, "pets"); |
| 852 | + Assert.assertEquals(cp1.baseType, "List"); |
| 853 | + Assert.assertEquals(cp1.getter, "getPets"); |
| 854 | + |
| 855 | + Assert.assertTrue(cm.imports.contains("List")); |
| 856 | + Assert.assertTrue(cm.imports.contains("Pet")); |
| 857 | + } |
| 858 | + |
| 859 | + @Test(description = "convert an array schema in a RequestBody") |
| 860 | + public void arraySchemaTestInRequestBody() { |
| 861 | + final Schema testSchema = new ArraySchema() |
| 862 | + .items(new Schema<>().$ref("#/components/schemas/Pet")); |
| 863 | + Operation operation = new Operation() |
| 864 | + .requestBody(new RequestBody() |
| 865 | + .content(new Content().addMediaType("application/json", |
| 866 | + new MediaType().schema(testSchema)))) |
| 867 | + .responses( |
| 868 | + new ApiResponses().addApiResponse("204", new ApiResponse() |
| 869 | + .description("Ok response"))); |
| 870 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 871 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 872 | + final CodegenOperation co = codegen.fromOperation("testSchema", "GET", operation, allDefinitions); |
| 873 | + |
| 874 | + Assert.assertEquals(co.bodyParams.size(), 1); |
| 875 | + CodegenParameter cp1 = co.bodyParams.get(0); |
| 876 | + Assert.assertEquals(cp1.baseType, "Pet"); |
| 877 | + Assert.assertEquals(cp1.dataType, "List<Pet>"); |
| 878 | + Assert.assertEquals(cp1.items.baseType, "Pet"); |
| 879 | + Assert.assertEquals(cp1.items.complexType, "Pet"); |
| 880 | + Assert.assertEquals(cp1.items.datatype, "List<Pet>"); |
| 881 | + |
| 882 | + Assert.assertEquals(co.responses.size(), 1); |
| 883 | + |
| 884 | + Assert.assertTrue(co.imports.contains("Pet")); |
| 885 | + } |
| 886 | + |
| 887 | + @Test(description = "convert an array schema in a ApiResponse") |
| 888 | + public void arraySchemaTestInOperationResponse() { |
| 889 | + final Schema testSchema = new ArraySchema() |
| 890 | + .items(new Schema<>().$ref("#/components/schemas/Pet")); |
| 891 | + Operation operation = new Operation().responses( |
| 892 | + new ApiResponses().addApiResponse("200", new ApiResponse() |
| 893 | + .description("Ok response") |
| 894 | + .content(new Content().addMediaType("application/json", |
| 895 | + new MediaType().schema(testSchema))))); |
| 896 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 897 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 898 | + final CodegenOperation co = codegen.fromOperation("testSchema", "GET", operation, allDefinitions); |
| 899 | + |
| 900 | + Assert.assertEquals(co.responses.size(), 1); |
| 901 | + CodegenResponse cr = co.responses.get(0); |
| 902 | + Assert.assertEquals(cr.baseType, "Pet"); |
| 903 | + Assert.assertEquals(cr.dataType, "List<Pet>"); |
| 904 | + Assert.assertEquals(cr.containerType, "array"); |
| 905 | + |
| 906 | + Assert.assertTrue(co.imports.contains("Pet")); |
| 907 | + } |
| 908 | + |
| 909 | + @Test(description = "convert a array of array schema") |
| 910 | + public void arrayOfArraySchemaTest() { |
| 911 | + final Schema testSchema = new ObjectSchema() |
| 912 | + .addProperties("pets", new ArraySchema() |
| 913 | + .items(new ArraySchema() |
| 914 | + .items(new Schema<>().$ref("#/components/schemas/Pet")))); |
| 915 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 916 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 917 | + final CodegenModel cm = codegen.fromModel("test", testSchema, allDefinitions); |
| 918 | + |
| 919 | + Assert.assertEquals(cm.vars.size(), 1); |
| 920 | + CodegenProperty cp1 = cm.vars.get(0); |
| 921 | + Assert.assertEquals(cp1.baseName, "pets"); |
| 922 | + Assert.assertEquals(cp1.datatype, "List<List<Pet>>"); |
| 923 | + Assert.assertEquals(cp1.name, "pets"); |
| 924 | + Assert.assertEquals(cp1.baseType, "List"); |
| 925 | + Assert.assertEquals(cp1.getter, "getPets"); |
| 926 | + |
| 927 | + Assert.assertTrue(cm.imports.contains("List")); |
| 928 | + Assert.assertTrue(cm.imports.contains("Pet")); |
| 929 | + } |
| 930 | + |
| 931 | + @Test(description = "convert an array of array schema in a RequestBody") |
| 932 | + public void arrayOfArraySchemaTestInRequestBody() { |
| 933 | + final Schema testSchema = new ArraySchema() |
| 934 | + .items(new ArraySchema() |
| 935 | + .items(new Schema<>().$ref("#/components/schemas/Pet"))); |
| 936 | + Operation operation = new Operation() |
| 937 | + .requestBody(new RequestBody() |
| 938 | + .content(new Content().addMediaType("application/json", |
| 939 | + new MediaType().schema(testSchema)))) |
| 940 | + .responses( |
| 941 | + new ApiResponses().addApiResponse("204", new ApiResponse() |
| 942 | + .description("Ok response"))); |
| 943 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 944 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 945 | + final CodegenOperation co = codegen.fromOperation("testSchema", "GET", operation, allDefinitions); |
| 946 | + |
| 947 | + Assert.assertEquals(co.bodyParams.size(), 1); |
| 948 | + CodegenParameter cp1 = co.bodyParams.get(0); |
| 949 | + Assert.assertEquals(cp1.baseType, "List"); |
| 950 | + Assert.assertEquals(cp1.dataType, "List<List<Pet>>"); |
| 951 | + Assert.assertEquals(cp1.items.baseType, "List"); |
| 952 | + Assert.assertEquals(cp1.items.complexType, "List"); |
| 953 | + Assert.assertEquals(cp1.items.datatype, "List<List<Pet>>"); |
| 954 | + |
| 955 | + Assert.assertEquals(co.responses.size(), 1); |
| 956 | + |
| 957 | + Assert.assertTrue(co.imports.contains("Pet")); |
| 958 | + } |
| 959 | + |
| 960 | + @Test(description = "convert a array schema in a ApiResponse") |
| 961 | + public void arrayOfArraySchemaTestInOperationResponse() { |
| 962 | + final Schema testSchema = new ArraySchema() |
| 963 | + .items(new ArraySchema() |
| 964 | + .items(new Schema<>().$ref("#/components/schemas/Pet"))); |
| 965 | + Operation operation = new Operation().responses( |
| 966 | + new ApiResponses().addApiResponse("200", new ApiResponse() |
| 967 | + .description("Ok response") |
| 968 | + .content(new Content().addMediaType("application/json", |
| 969 | + new MediaType().schema(testSchema))))); |
| 970 | + final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema()); |
| 971 | + final DefaultCodegenConfig codegen = new JavaClientCodegen(); |
| 972 | + final CodegenOperation co = codegen.fromOperation("testSchema", "GET", operation, allDefinitions); |
| 973 | + |
| 974 | + Assert.assertEquals(co.responses.size(), 1); |
| 975 | + CodegenResponse cr = co.responses.get(0); |
| 976 | + Assert.assertEquals(cr.baseType, "Pet"); |
| 977 | + Assert.assertEquals(cr.dataType, "List<List<Pet>>"); |
| 978 | + Assert.assertEquals(cr.containerType, "array"); |
| 979 | + |
| 980 | + Assert.assertTrue(co.imports.contains("Pet")); |
| 981 | + } |
| 982 | + |
828 | 983 | @Test(enabled = false, description = "disabled since templates have been moved.")
|
829 | 984 | public void generateModel() throws Exception {
|
830 | 985 | folder.create();
|
|
0 commit comments