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: YAML Generation #108

Open
pta200 opened this issue Apr 26, 2024 · 1 comment
Open

Bug: YAML Generation #108

pta200 opened this issue Apr 26, 2024 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@pta200
Copy link

pta200 commented Apr 26, 2024

Expected Behaviour

Following the example from the readme I would expect to be able to do generate YAML as easily as JSON.
obj = model_to_object(model=job_template)
print(yaml.safe_dump(obj))

Current Behaviour

Current behavior is that pyyaml throws the following exception processing the job name.

> Traceback (most recent call last):
  File "/mnt/home/paolo.audiberti/project-src/farmtests/farmtests/model_gen_job.py", line 92, in <module>
    print(yaml.safe_dump(model_to_object(model=job_template)))
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/__init__.py", line 269, in safe_dump
    return dump_all([data], stream, Dumper=SafeDumper, **kwds)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/__init__.py", line 241, in dump_all
    dumper.represent(data)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 27, in represent
    node = self.represent_data(data)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 48, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 207, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 118, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 58, in represent_data
    node = self.yaml_representers[None](self, data)
  File "/mnt/home/paolo.audiberti/project-src/farmtests/.venv/farmtests-cLCMXaGk-py3.9/lib64/python3.9/site-packages/yaml/representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', 'DemoJob')

Reproduction Steps

Run the included code snipet

Code Snippet

import json
import yaml
import uuid
from pathlib import Path

from openjd.model import (
    decode_job_template,
    model_to_object,
    DocumentType,
    
    
)
from openjd.model.v2023_09 import *


  job_template_path = Path("/test-job/template.yaml")
  job_template = JobTemplate(
      specificationVersion="jobtemplate-2023-09",
      name="DemoJob",
      parameterDefinitions=[
          JobPathParameterDefinition(
              name="InFile",
              type=JobParameterType.PATH,
              objectType="FILE",
              dataFlow="IN",
              ),
          JobPathParameterDefinition(
              name="OutFile",
              type=JobParameterType.PATH,
              objectType="FILE",
              dataFlow="OUT",
              )
          ],
      steps=[
          StepTemplate(
              name="RenderStep",
              script=StepScript(
                  actions=StepActions(
                      onRun=Action(
                          command="{{Task.File.Run}}"
                      )
                  ),
                  embeddedFiles=[
                      EmbeddedFileText(
                      name="Run",
                      type=EmbeddedFileTypes.TEXT,
                      data="#!/usr/bin/env bash \n echo 'openjd_status: START JOB' ;  sha256sum '{{Param.InFile}}' > '{{Param.OutFile}}'  ",
                      runnable=True
                      )
                  ]
              )
          )
      ]
  )

  print(yaml.safe_dump(model_to_object(model=job_template)))
@pta200 pta200 added the bug Something isn't working label Apr 26, 2024
@ddneilson
Copy link
Contributor

ddneilson commented Apr 26, 2024

Thanks for the report!

From the stack trace (node = self.yaml_representers[None](self, data)), it looks like we're leaving some None values in the object and the yaml encoder doesn't like that.

It may be related to this code. We already delete None valued keys, but don't do so if the value is an array element. So, I'd wager that we have an array containing None values in the data model, for some reason.

Assignee will want to dig into what exactly the resulting model is and figure out where the None values are to narrow in on a fix.

@ddneilson ddneilson added the good first issue Good for newcomers label Apr 26, 2024
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