22
33import io .swagger .parser .OpenAPIParser ;
44import io .swagger .v3 .oas .models .OpenAPI ;
5+ import io .swagger .v3 .oas .models .media .ArraySchema ;
6+ import io .swagger .v3 .oas .models .media .ComposedSchema ;
7+ import io .swagger .v3 .oas .models .media .MapSchema ;
8+ import io .swagger .v3 .oas .models .media .ObjectSchema ;
9+ import io .swagger .v3 .oas .models .media .Schema ;
10+ import io .swagger .v3 .oas .models .media .StringSchema ;
11+ import io .swagger .v3 .oas .models .media .UUIDSchema ;
512import io .swagger .v3 .parser .core .models .ParseOptions ;
613import org .openapitools .codegen .ClientOptInput ;
14+ import org .openapitools .codegen .CodegenModel ;
15+ import org .openapitools .codegen .CodegenProperty ;
716import org .openapitools .codegen .DefaultCodegen ;
817import org .openapitools .codegen .DefaultGenerator ;
918import org .openapitools .codegen .languages .PythonFlaskConnexionServerCodegen ;
1019import org .openapitools .codegen .languages .features .CXFServerFeatures ;
1120import org .testng .Assert ;
21+ import org .testng .annotations .DataProvider ;
1222import org .testng .annotations .Test ;
1323
1424import java .io .File ;
2030
2131import static org .openapitools .codegen .TestUtils .assertFileContains ;
2232import static org .openapitools .codegen .TestUtils .assertFileExists ;
33+ import static org .openapitools .codegen .TestUtils .assertFileNotContains ;
2334
2435public class PythonFlaskConnexionServerCodegenTest {
2536
@@ -45,6 +56,70 @@ static private String generateFiles(DefaultCodegen codegen, String filePath) thr
4556 }
4657
4758
59+ @ Test (description = "UUID model properties require the standard-library UUID import (issue #23897)" )
60+ public void testUuidModelImport () throws IOException {
61+ final DefaultCodegen codegen = new PythonFlaskConnexionServerCodegen ();
62+ final String outputPath = generateFiles (codegen , "src/test/resources/bugs/issue_23897.yaml" );
63+
64+ final Path model = Paths .get (outputPath , "openapi_server/models/pushnotification.py" );
65+ assertFileExists (model );
66+ assertFileContains (model , "from uuid import UUID" , "'id': UUID" , "def id(self) -> UUID:" );
67+
68+ final String [][] uuidModels = {
69+ {"uuid_list.py" , "List[UUID]" },
70+ {"uuid_map.py" , "Dict[str, UUID]" },
71+ {"nested_uuid_containers.py" , "List[Dict[str, List[UUID]]]" },
72+ {"all_of_uuid.py" , "UUID" }
73+ };
74+ for (String [] uuidModel : uuidModels ) {
75+ final Path generatedModel = Paths .get (outputPath , "openapi_server/models" , uuidModel [0 ]);
76+ assertFileExists (generatedModel );
77+ assertFileContains (generatedModel , "from uuid import UUID" , "'ids': " + uuidModel [1 ],
78+ "def ids(self) -> " + uuidModel [1 ] + ":" );
79+ }
80+
81+ final Path stringList = Paths .get (outputPath , "openapi_server/models/string_list.py" );
82+ assertFileContains (stringList , "'ids': List[str]" );
83+ assertFileNotContains (stringList , "from uuid import UUID" );
84+ }
85+
86+ @ DataProvider
87+ public Object [][] uuidSchemas () {
88+ return new Object [][] {
89+ {"direct UUID" , new UUIDSchema (), true },
90+ {"array items" , new ArraySchema ().items (new UUIDSchema ()), true },
91+ {"map values" , new MapSchema ().additionalProperties (new UUIDSchema ()), true },
92+ {"UUID beside another object" , new ObjectSchema ()
93+ .addProperties ("id" , new UUIDSchema ())
94+ .addProperties ("tail" , new ObjectSchema ().addProperties ("name" , new StringSchema ())), true },
95+ {"allOf branch" , new ComposedSchema ().addAllOfItem (new StringSchema ())
96+ .addAllOfItem (new UUIDSchema ()), true },
97+ {"oneOf branch" , new ComposedSchema ().addOneOfItem (new StringSchema ())
98+ .addOneOfItem (new UUIDSchema ()), true },
99+ {"anyOf branch" , new ComposedSchema ().addAnyOfItem (new StringSchema ())
100+ .addAnyOfItem (new UUIDSchema ()), true },
101+ {"not branch" , new Schema ().not (new UUIDSchema ()), true },
102+ {"mixed nesting" , new ArraySchema ().items (new ObjectSchema ().addProperties ("value" ,
103+ new ComposedSchema ().addAnyOfItem (new StringSchema ())
104+ .addAnyOfItem (new MapSchema ().additionalProperties (new UUIDSchema ())))), true },
105+ {"no UUID" , new ArraySchema ().items (new ObjectSchema ().addProperties ("value" ,
106+ new ComposedSchema ().addAnyOfItem (new StringSchema ())
107+ .addAnyOfItem (new MapSchema ().additionalProperties (new StringSchema ())))), false }
108+ };
109+ }
110+
111+ @ Test (dataProvider = "uuidSchemas" , description = "Find UUIDs in every schema branch regardless of feature support" )
112+ public void testUuidImportInNestedSchemas (String description , Schema schema , boolean expectedImport ) {
113+ final PythonFlaskConnexionServerCodegen codegen = new PythonFlaskConnexionServerCodegen ();
114+ codegen .setOpenAPI (new OpenAPI ());
115+ final CodegenProperty property = codegen .fromProperty ("value" , schema , false );
116+ final CodegenModel model = new CodegenModel ();
117+
118+ codegen .postProcessModelProperty (model , property );
119+
120+ Assert .assertEquals (model .imports .contains ("from uuid import UUID" ), expectedImport , description );
121+ }
122+
48123 @ Test (description = "test requestBody" )
49124 public void testRequestBody () throws IOException {
50125 final DefaultCodegen codegen = new PythonFlaskConnexionServerCodegen ();
0 commit comments