OpenJobDescription / openjd-model-for-python

Provides a Python implementation of the data model for Open Job Description's template schemas.
https://github.com/OpenJobDescription/openjd-specifications/wiki
Apache License 2.0
12 stars 5 forks source link

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

Open lucaseck opened 2 months ago

lucaseck commented 2 months ago

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 commented 1 month ago

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 commented 1 month ago

The fix for this should be a fairly simple matter of adding more cases to the dictionary value conversion that is happening in https://github.com/OpenJobDescription/openjd-model-for-python/blob/cec9c3cad94480eed949c98cb9cffcbce07ca84b/src/openjd/model/_parse.py#L110-L130

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).