|
26 | 26 | import org.openapitools.codegen.model.ModelMap; |
27 | 27 | import org.openapitools.codegen.model.OperationMap; |
28 | 28 | import org.openapitools.codegen.model.OperationsMap; |
| 29 | +import org.openapitools.codegen.utils.ModelUtils; |
29 | 30 | import org.testng.Assert; |
30 | 31 | import org.testng.annotations.Test; |
31 | 32 |
|
32 | 33 | import java.io.File; |
33 | 34 | import java.nio.charset.StandardCharsets; |
34 | 35 | import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
35 | 37 | import java.util.*; |
36 | 38 | import java.util.stream.Collectors; |
37 | 39 |
|
@@ -799,4 +801,61 @@ public void testQueryParamJsonSerializationSetsQueryIsJsonMimeType() { |
799 | 801 | assertTrue(op.queryParams.stream().allMatch(p -> p.queryIsJsonMimeType), |
800 | 802 | "All content:application/json query params should have queryIsJsonMimeType=true"); |
801 | 803 | } |
| 804 | + |
| 805 | + @Test(description = "an allOf child's build_from_hash and to_hash include the attributes inherited from its parents") |
| 806 | + public void testBuildFromHashMapsInheritedAttributes() throws Exception { |
| 807 | + final File output = Files.createTempDirectory("test").toFile(); |
| 808 | + output.deleteOnExit(); |
| 809 | + |
| 810 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/allOf_composition_discriminator.yaml"); |
| 811 | + CodegenConfig codegenConfig = new RubyClientCodegen(); |
| 812 | + codegenConfig.setOutputDir(output.getAbsolutePath()); |
| 813 | + |
| 814 | + ClientOptInput clientOptInput = new ClientOptInput().openAPI(openAPI).config(codegenConfig); |
| 815 | + new DefaultGenerator().opts(clientOptInput).generate(); |
| 816 | + |
| 817 | + // Lizard < Reptile < Pet: types and nullability merge along the ancestry, the nearest declaration wins |
| 818 | + Path lizard = new File(output, "lib/openapi_client/models/lizard.rb").toPath(); |
| 819 | + TestUtils.assertFileContains(lizard, |
| 820 | + "superclass.acceptable_openapi_types.merge(openapi_types)", |
| 821 | + "(superclass.acceptable_openapi_nullable - attribute_map.keys) | openapi_nullable"); |
| 822 | + // build_from_hash and to_hash use the merged maps |
| 823 | + TestUtils.assertFileContains(lizard, |
| 824 | + "acceptable_openapi_types.each_pair do |key, type|", |
| 825 | + "self.class.acceptable_attribute_map.each_pair do |attr, param|", |
| 826 | + "is_nullable = self.class.acceptable_openapi_nullable.include?(attr)"); |
| 827 | + // the super call that built and discarded a second instance is gone |
| 828 | + TestUtils.assertFileNotContains(lizard, "super(attributes)\n attributes = attributes.transform_keys(&:to_sym)"); |
| 829 | + // a root model has no parent to merge |
| 830 | + TestUtils.assertFileNotContains(new File(output, "lib/openapi_client/models/pet.rb").toPath(), "superclass.acceptable_openapi_types"); |
| 831 | + // a generated parent still receives the initializer call |
| 832 | + TestUtils.assertFileContains(lizard, "# call parent's initialize\n super(attributes)"); |
| 833 | + } |
| 834 | + |
| 835 | + @Test(description = "a model whose superclass is not a generated model (an alias to an array) does not delegate to it") |
| 836 | + public void testAliasModelDoesNotDelegateToItsBuiltInSuperclass() throws Exception { |
| 837 | + final File output = Files.createTempDirectory("test").toFile(); |
| 838 | + output.deleteOnExit(); |
| 839 | + |
| 840 | + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/features/generate-alias-as-model.yaml"); |
| 841 | + CodegenConfig codegenConfig = new RubyClientCodegen(); |
| 842 | + codegenConfig.setOutputDir(output.getAbsolutePath()); |
| 843 | + |
| 844 | + ClientOptInput clientOptInput = new ClientOptInput().openAPI(openAPI).config(codegenConfig); |
| 845 | + ModelUtils.setGenerateAliasAsModel(true); |
| 846 | + try { |
| 847 | + new DefaultGenerator().opts(clientOptInput).generate(); |
| 848 | + } finally { |
| 849 | + ModelUtils.setGenerateAliasAsModel(false); |
| 850 | + } |
| 851 | + |
| 852 | + // ArrayAlias < Array: Array has none of the helpers, so each one is its own base case |
| 853 | + Path arrayAlias = new File(output, "lib/openapi_client/models/array_alias.rb").toPath(); |
| 854 | + TestUtils.assertFileContains(arrayAlias, |
| 855 | + "class ArrayAlias < Array", |
| 856 | + "def self.acceptable_attribute_map\n attribute_map\n end", |
| 857 | + "def self.acceptable_openapi_types\n openapi_types\n end", |
| 858 | + "def self.acceptable_openapi_nullable\n openapi_nullable\n end"); |
| 859 | + TestUtils.assertFileNotContains(arrayAlias, "superclass.", "super(attributes)"); |
| 860 | + } |
802 | 861 | } |
0 commit comments