Skip to content
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

Bug: Template to YAML leaves unevaluated objects in the template #125

Open
lucaseck opened this issue Jun 18, 2024 · 2 comments
Open

Bug: Template to YAML leaves unevaluated objects in the template #125

lucaseck opened this issue Jun 18, 2024 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@lucaseck
Copy link

Expected Behaviour

When trying to print a job template to YAML after calling model_to_object and then using yaml.dump the template is properly formatted and a valid template.

Current Behaviour

When trying to print a job template to YAML after calling model_to_object and then using yaml.dump the template contains what look like objects instead of the strings that those objects are supposed to contain.

Reproduction Steps

Ran the code snippet linked below.

Code Snippet

parameters: JobParameterDefinitionList = []
parameters.append(
    JobPathParameterDefinition(
        name="Parameter1",
        default="C:\\Users\\testing\\Desktop",
        type=JobParameterType.PATH,
    )
)

template = JobTemplate(
        name="bug template",
        specificationVersion="jobtemplate-2023-09",
        parameterDefinitions=parameters,
        steps=[
            StepTemplate(
                name="Main Step",
                script=StepScript(
                    actions=StepActions(onRun=Action(command="echo", args=["hello"]))
                ),
            )
        ],
    )
template_obj = model_to_object(model=template)
print(yaml.dump(template_obj))

Results in:

name: !!python/object/new:openjd.model.v2023_09._model.JobTemplateName
  args:
  - bug template
  state:
    _processed_list:
    - bug template
parameterDefinitions:
- default: C:\Users\testing\Desktop
  name: Parameter1
  type: !!python/object/apply:openjd.model.v2023_09._model.JobParameterType
  - PATH
specificationVersion: !!python/object/apply:openjd.model._types.TemplateSpecificationVersion
- jobtemplate-2023-09
steps:
- name: Main Step
  script:
    actions:
      onRun:
        args:
        - !!python/object/new:openjd.model.v2023_09._model.ArgString
          args:
          - hello
          state:
            _processed_list:
            - hello
        command: !!python/object/new:openjd.model.v2023_09._model.CommandString
          args:
          - echo
          state:
            _processed_list:
            - echo
@lucaseck lucaseck added the bug Something isn't working label Jun 18, 2024
@lucaseck
Copy link
Author

lucaseck commented Jul 4, 2024

Impact here is that the openjd-model-for-python can't be used to programmatically create valid yaml job templates that can be saved out.

@ddneilson ddneilson added the good first issue Good for newcomers label Jul 11, 2024
@ddneilson
Copy link
Contributor

The fix for this should be a fairly simple matter of adding more cases to the dictionary value conversion that is happening in

# Some of the values in the model can be type 'Decimal', which doesn't
# encode into json/yaml without special handling. So, we convert those in to
# strings.
def decimal_to_str(data: Union[dict[str, Any], list[Any]]) -> None:
if isinstance(data, list):
for i, item in enumerate(data):
if isinstance(item, Decimal):
data[i] = str(item)
elif isinstance(item, (dict, list)):
decimal_to_str(item)
else:
delete_keys: list[str] = []
for k, v in data.items():
if isinstance(v, Decimal):
data[k] = str(v)
elif isinstance(v, (dict, list)):
decimal_to_str(v)
elif v is None:
delete_keys.append(k)
for k in delete_keys:
del data[k]

That processing should be ensuring that all of the values returned in the dictionary are basic types (e.g. str rather than something derived from str).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants