-
Notifications
You must be signed in to change notification settings - Fork 861
feat(config): add additionalProperties support to generated config models #5131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MikeGoldsmith
wants to merge
8
commits into
open-telemetry:main
Choose a base branch
from
MikeGoldsmith:mike/config-additional-properties-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0b58d91
add additional_properties support to generated config models
MikeGoldsmith f5177f1
update CHANGELOG with PR number #5131
MikeGoldsmith 830b8db
merge upstream/main
MikeGoldsmith 19f9759
fix CI: plain assignment, pylint suppression for intentional unknown …
MikeGoldsmith 965b6b0
Merge branch 'main' into mike/config-additional-properties-support
MikeGoldsmith 9a51e6c
use ClassVar annotation instead of dataclass field for additional_pro…
MikeGoldsmith 47f2d27
Merge remote-tracking branch 'origin/mike/config-additional-propertie…
MikeGoldsmith 7e58f80
Merge branch 'main' into mike/config-additional-properties-support
MikeGoldsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Code Generation Templates | ||
|
|
||
| Custom [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator) templates used when generating `models.py` from the OpenTelemetry configuration JSON schema. | ||
|
|
||
| ## `dataclass.jinja2` | ||
|
|
||
| Extends the default dataclass template to support `additionalProperties` from the JSON Schema. Schema types that allow additional properties (e.g. `Sampler`, `SpanExporter`, `TextMapPropagator`) get: | ||
|
|
||
| - `@_additional_properties_support` decorator — captures unknown constructor kwargs | ||
| - `additional_properties: dict[str, Any]` field — stores the captured values | ||
|
|
||
| This enables plugin/custom component names to flow through typed dataclasses without a post-processing step. | ||
|
|
||
| ## Usage | ||
|
|
||
| Templates are applied automatically when regenerating models: | ||
|
|
||
| ```sh | ||
| tox -e generate-config-from-jsonschema | ||
| ``` | ||
|
|
||
| The template directory is configured in `pyproject.toml` under `[tool.datamodel-codegen]`. | ||
|
|
||
| See `opentelemetry-sdk/src/opentelemetry/sdk/_configuration/README.md` for the full schema update workflow. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| {# Custom dataclass template for OpenTelemetry configuration models. | ||
| Extends the default datamodel-codegen dataclass template to support | ||
| JSON Schema additionalProperties. When a schema type allows additional | ||
| properties (e.g. Sampler, SpanExporter), this template adds: | ||
| - @_additional_properties_support decorator (captures unknown kwargs) | ||
| - additional_properties: dict[str, Any] field | ||
|
|
||
| The template checks two context variables set by datamodel-codegen: | ||
| - additionalProperties: set when the schema value is a boolean (true/false) | ||
| - additionalPropertiesType: set when the schema value is a type object | ||
| (e.g. {"type": ["object", "null"]}), which is how the OTel config | ||
| schema defines it for plugin-capable types. | ||
|
|
||
| See TestGeneratedModelsHaveAdditionalProperties in test_common.py for | ||
| regression tests that guard against changes in these context variables. | ||
| -#} | ||
| {% for decorator in decorators -%} | ||
| {{ decorator }} | ||
| {% endfor -%} | ||
| {%- set args = [] %} | ||
| {%- for k, v in (dataclass_arguments or {}).items() %} | ||
| {%- if v is not none and v is not false %} | ||
| {%- set _ = args.append(k ~ '=' ~ (v|pprint)) %} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- set has_additional = (additionalProperties is defined and additionalProperties != false) or (additionalPropertiesType is defined) %} | ||
| {%- if has_additional %} | ||
| @_additional_properties_support | ||
| {%- endif %} | ||
| {%- if args %} | ||
| @dataclass({{ args | join(', ') }}) | ||
| {%- else %} | ||
| @dataclass | ||
| {%- endif %} | ||
| {%- if base_class %} | ||
| class {{ class_name }}({{ base_class }}): | ||
| {%- else %} | ||
| class {{ class_name }}: | ||
| {%- endif %} | ||
| {%- if description %} | ||
| """ | ||
| {{ description | escape_docstring | indent(4) }} | ||
| """ | ||
| {%- endif %} | ||
| {%- if not fields and not description and not has_additional %} | ||
| pass | ||
| {%- endif %} | ||
| {%- for field in fields -%} | ||
| {%- if field.field %} | ||
| {{ field.name }}: {{ field.type_hint }} = {{ field.field }} | ||
| {%- else %} | ||
| {{ field.name }}: {{ field.type_hint }} | ||
| {%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none)) | ||
| %} = {{ field.represented_default }} | ||
| {%- endif -%} | ||
| {%- endif %} | ||
| {%- if field.docstring %} | ||
| """ | ||
| {{ field.docstring | escape_docstring | indent(4) }} | ||
| """ | ||
| {%- if field.use_inline_field_description and not loop.last %} | ||
|
|
||
| {% endif %} | ||
| {%- elif field.inline_field_docstring %} | ||
| {{ field.inline_field_docstring }} | ||
| {%- if not loop.last %} | ||
|
|
||
| {% endif %} | ||
| {%- endif %} | ||
| {%- endfor -%} | ||
| {%- if has_additional %} | ||
| additional_properties: dict[str, Any] = field(default_factory=dict) | ||
| {%- endif -%} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.