xoofx / xoofx/SharpYaml

Reference processing outputs content instead of original reference

Open
#155 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
413
Forks
72
PR merge metrics
No merged PRs in 30d

Description

There is a small issue with reference processing in that "round tripping" a valid YAML file with a reference in a slightly different place causes the whole content to be output rather than the reference. The configuration is needed for Docker compose.yaml files where you want both the referenced data and additional values.

In the sample here, the common-environment is the section that gets copied in full, rather than staying as a reference.

The original YAML:

name: MYDOCKER

x-common-volumes: &common-volumes
  stop_grace_period: 15s
  volumes:
    - /highlight:/ABetA
    - /highlight:/highlight

x-common-environment: 
  environment: &common-environment
    DOTNET_GCHeapHardLimit: 0x40000000 
    DOTNET_GCHighMemPercent: 70
    DOTNET_GCConserveMemory: 9
    DOTNET_gcServer: 0
    DOTNET_TieredCompilation: 1
    
x-common-memory: &common-memory
  mem_limit: 2g
  memswap_limit: 2g 
  mem_reservation: 1g
  cpus: 2

services:

    designer:
        profiles: ["base"]
        ports:
            - 9001:9001
        hostname: my-designer
        container_name: my-designer
        deploy:
            resources:
                limits:
                    memory: 2GB
                    cpus: '4'
        logging:
            driver: "local"
            options:
                max-file: 3
                compress: "false"
        <<: *common-volumes
        environment:
            <<: *common-environment
            DataSuffix: 1
        image: mycr.azurecr.io/mydesigner:develop
        depends_on:
            rabbitmq:
                condition: service_healthy
       

The round-trip YAML:

&id001
name: MYDOCKER
x-common-volumes: &id002
  stop_grace_period: 15s
  volumes: &id003
    - "/highlight:/ABetA"
    - "/highlight:/highlight"
x-common-environment: &id004
  environment: &id005
    DOTNET_GCHeapHardLimit: 1073741824
    DOTNET_GCHighMemPercent: 70
    DOTNET_GCConserveMemory: 9
    DOTNET_gcServer: 0
    DOTNET_TieredCompilation: 1
x-common-memory: &id006
  mem_limit: 2g
  memswap_limit: 2g
  mem_reservation: 1g
  cpus: 2
services: &id007
  designer: &id008
    profiles: &id009
      - base
    ports: &id010
      - "9001:9001"
    hostname: my-designer
    container_name: my-designer
    deploy: &id011
      resources: &id012
        limits: &id013
          memory: 2GB
          cpus: "4"
    logging: &id014
      driver: local
      options: &id015
        max-file: 3
        compress: "false"
    stop_grace_period: 15s
    volumes: *id003
    environment: &id016
      DOTNET_GCHeapHardLimit: 1073741824
      DOTNET_GCHighMemPercent: 70
      DOTNET_GCConserveMemory: 9
      DOTNET_gcServer: 0
      DOTNET_TieredCompilation: 1
      DataSuffix: 1
    image: "mycr.azurecr.io/mydesigner:develop"
    depends_on: &id017
      rabbitmq: &id018
        condition: service_healthy

Code to do the transform:

   public void LoadAndSaveYaml(string filename)
   {
       var yaml = File.ReadAllText(filename);

       var options = new YamlSerializerOptions
       {
           ReferenceHandling = YamlReferenceHandling.Preserve,
       };

       var yamlObject = YamlSerializer.Deserialize<object>(yaml, options);
       string saveFile = System.IO.Path.ChangeExtension(filename, "txt");
       var yamlText = YamlSerializer.Serialize(yamlObject, options);
       File.WriteAllText(saveFile, yamlText);
   }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the LoadAndSaveYaml example and the YamlReferenceHandling.Preserve option, then reproduce the transformation using the supplied Docker Compose YAML. Check why the environment reference is expanded during serialization; done means the round-tripped output retains the reference instead of copying the full content.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.