From c666f03041683de03f6acf0cd25b5d771fb87d5a Mon Sep 17 00:00:00 2001 From: timonrieger Date: Thu, 12 Feb 2026 00:03:23 +0100 Subject: [PATCH] import field_validator for UUID properties with patterns --- .../codegen/languages/AbstractPythonCodegen.java | 4 ++++ .../codegen/python/PythonClientCodegenTest.java | 10 ++++++++++ .../resources/bugs/issue_uuid_with_pattern.yaml | 14 ++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_uuid_with_pattern.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index 0f43180be5a0..a4c775057c47 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -2064,6 +2064,10 @@ private PythonType fromCommon(IJsonSchemaValidationProperties cp) { moduleImports.add("pydantic", "field_validator"); } + if (cp.getPattern() != null) { + moduleImports.add("pydantic", "field_validator"); + } + if (cp.getIsArray()) { return arrayType(cp); } else if (cp.getIsMap() || cp.getIsFreeFormObject()) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java index a18f2f40d11b..0ec48cfb7072 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java @@ -660,6 +660,16 @@ public void testPoetry1LicenseFormat() throws IOException { TestUtils.assertFileNotContains(pyprojectPath, "license = { text = \"Apache-2.0\" }"); } + @Test(description = "UUID property with pattern should import field_validator") + public void testUuidWithPatternImportsFieldValidator() throws IOException { + final DefaultCodegen codegen = new PythonClientCodegen(); + final String outputPath = generateFiles(codegen, "src/test/resources/bugs/issue_uuid_with_pattern.yaml"); + final Path p = Paths.get(outputPath + "openapi_client/models/uuid_with_pattern.py"); + + assertFileExists(p); + assertFileContains(p, "from pydantic import BaseModel, ConfigDict, field_validator"); + } + @Test(description = "Verify non-poetry1 mode uses object notation for license") public void testNonPoetry1LicenseFormat() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_uuid_with_pattern.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_uuid_with_pattern.yaml new file mode 100644 index 000000000000..0ad4a72487b1 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_uuid_with_pattern.yaml @@ -0,0 +1,14 @@ +openapi: 3.0.0 +info: + title: Test UUID with pattern + version: 1.0.0 +paths: {} +components: + schemas: + UuidWithPattern: + type: object + properties: + id: + type: string + format: uuid + 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}$"