Skip to content

Commit a2b7776

Browse files
authored
import field_validator for UUID properties with patterns (#22947)
1 parent 7b59bdd commit a2b7776

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,10 @@ private PythonType fromCommon(IJsonSchemaValidationProperties cp) {
20642064
moduleImports.add("pydantic", "field_validator");
20652065
}
20662066

2067+
if (cp.getPattern() != null) {
2068+
moduleImports.add("pydantic", "field_validator");
2069+
}
2070+
20672071
if (cp.getIsArray()) {
20682072
return arrayType(cp);
20692073
} else if (cp.getIsMap() || cp.getIsFreeFormObject()) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,16 @@ public void testPoetry1LicenseFormat() throws IOException {
660660
TestUtils.assertFileNotContains(pyprojectPath, "license = { text = \"Apache-2.0\" }");
661661
}
662662

663+
@Test(description = "UUID property with pattern should import field_validator")
664+
public void testUuidWithPatternImportsFieldValidator() throws IOException {
665+
final DefaultCodegen codegen = new PythonClientCodegen();
666+
final String outputPath = generateFiles(codegen, "src/test/resources/bugs/issue_uuid_with_pattern.yaml");
667+
final Path p = Paths.get(outputPath + "openapi_client/models/uuid_with_pattern.py");
668+
669+
assertFileExists(p);
670+
assertFileContains(p, "from pydantic import BaseModel, ConfigDict, field_validator");
671+
}
672+
663673
@Test(description = "Verify non-poetry1 mode uses object notation for license")
664674
public void testNonPoetry1LicenseFormat() throws IOException {
665675
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test UUID with pattern
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
UuidWithPattern:
9+
type: object
10+
properties:
11+
id:
12+
type: string
13+
format: uuid
14+
pattern: "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"

0 commit comments

Comments
 (0)