Skip to content

Commit

Permalink
Support Python codegen for the OpenAPI backend (#21316)
Browse files Browse the repository at this point in the history
  • Loading branch information
grihabor authored Dec 22, 2024
1 parent a3daa31 commit 0e0fcdd
Show file tree
Hide file tree
Showing 16 changed files with 1,188 additions and 25 deletions.
5 changes: 5 additions & 0 deletions docs/notes/2.25.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Reverence to Python Build Standalone not refer to the [GitHub organization](http

The default version of the [Pex](https://docs.pex-tool.org/) tool has been updated from 2.20.3 to [2.27.1](https://github.com/pex-tool/pex/releases/tag/v2.24.3). Among many improvements and bug fixes, this unlocks support for pip [24.3.1](https://pip.pypa.io/en/stable/news/#v24-3-1).

##### NEW: Python for OpenAPI

A new experimental `pants.backend.experimental.openapi.codegen.python` backend
was added to support python codegen for OpenAPI documents.

#### Shell

The previously deprecated `[shell-setup].tailor` option has now been removed. See [`[shell-setup].tailor_sources`](https://www.pantsbuild.org/2.25/reference/subsystems/shell-setup#tailor_sources) and [`[shell-setup].tailor_shunit2_tests`](https://www.pantsbuild.org/2.25/reference/subsystems/shell#tailor_shunit2_tests) to update.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from pants.backend.experimental.openapi.register import rules as openapi_rules
from pants.backend.experimental.openapi.register import target_types as openapi_target_types
from pants.backend.experimental.python.register import rules as python_rules
from pants.backend.experimental.python.register import target_types as python_target_types
from pants.backend.openapi.codegen.python.rules import rules as openapi_python_codegen_rules


def target_types():
return [*python_target_types(), *openapi_target_types()]


def rules():
return [*python_rules(), *openapi_rules(), *openapi_python_codegen_rules()]
7 changes: 2 additions & 5 deletions src/python/pants/backend/openapi/codegen/java/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
OpenApiSourceField,
)
from pants.backend.openapi.util_rules import generator_process, pom_parser
from pants.backend.openapi.util_rules.generator_process import (
OpenAPIGeneratorProcess,
OpenAPIGeneratorType,
)
from pants.backend.openapi.util_rules.generator_process import OpenAPIGeneratorProcess
from pants.backend.openapi.util_rules.pom_parser import AnalysePomRequest, PomReport
from pants.engine.fs import (
EMPTY_SNAPSHOT,
Expand Down Expand Up @@ -106,7 +103,7 @@ async def compile_openapi_into_java(
merged_digests = await Get(Digest, MergeDigests([request.input_digest, output_digest]))

process = OpenAPIGeneratorProcess(
generator_type=OpenAPIGeneratorType.JAVA,
generator_name="java",
argv=[
*(
("--additional-properties", f"apiPackage={request.api_package}")
Expand Down
8 changes: 8 additions & 0 deletions src/python/pants/backend/openapi/codegen/python/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()

python_tests(name="tests", dependencies=[":lockfiles"])

resources(name="lockfiles", sources=["*.test.lock"])
Empty file.
42 changes: 42 additions & 0 deletions src/python/pants/backend/openapi/codegen/python/extra_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import itertools

from pants.backend.openapi.target_types import OpenApiDocumentGeneratorTarget, OpenApiDocumentTarget
from pants.backend.python.target_types import PrefixedPythonResolveField
from pants.engine.target import BoolField, DictStringToStringField, StringField


class OpenApiPythonGeneratorNameField(StringField):
alias = "python_generator_name"
required = False
help = "Python generator name"


class OpenApiPythonAdditionalPropertiesField(DictStringToStringField):
alias = "python_additional_properties"
help = "Additional properties for python generator"


class OpenApiPythonSkipField(BoolField):
alias = "skip_python"
default = False
help = "If true, skips generation of Python sources from this target"


def rules():
return [
target.register_plugin_field(field)
for target, field in itertools.product(
(
OpenApiDocumentTarget,
OpenApiDocumentGeneratorTarget,
),
(
OpenApiPythonSkipField,
OpenApiPythonGeneratorNameField,
OpenApiPythonAdditionalPropertiesField,
PrefixedPythonResolveField,
),
)
]
Loading

0 comments on commit 0e0fcdd

Please sign in to comment.