Skip to content

Reference processing outputs content instead of original reference #155

Description

@mj2015

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);
   }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions