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

Unwanted conversion from a string to a numeric value #826

Closed
NCC1701M opened this issue Jul 12, 2023 · 1 comment
Closed

Unwanted conversion from a string to a numeric value #826

NCC1701M opened this issue Jul 12, 2023 · 1 comment

Comments

@NCC1701M
Copy link

Describe the bug
I'm trying to update a docker compose file. The file contains a property called version. The value of this property is set to "3". I'm reading the file, make some changes and save the changed data in the same file. But now the value of version is no longer the string representation of the version instead it is a numeric value.

This makes the docker compose call fail because docker compose expects the value of the version file to be a string value.

To Reproduce
Use the following docker compose file:

version: "3"

services:
  app:
    container_name: MyApp
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
    - ..:/workspaces:cached
    network_mode: service:db
    command: sleep infinity

  db:
    container_name: MyDB
    image: mcr.microsoft.com/mssql/server:2022-latest
    restart: unless-stopped
    environment:
      ACCEPT_EULA: Y
      COLLATION: Latin1_General_CI_AS
      SA_PASSWORD: SomePassword

Read the file using the following code:

protected IDictionary ReadYaml(string composePath)
{
	var serializer = new DeserializerBuilder().Build();
	using (var reader = new StreamReader(composePath))
	{
		return serializer.Deserialize(reader) as IDictionary;
	}
}

Make some changes to the yaml like:

var services = dockerComp["services"] as IDictionary;
services["anotherservice"] = new
{
  container_name = "AnotherService",
  build = new
  {
    context = ".",
    dockerfile = "Dockerfile.another"
  },
  network_mode = "service:db"
};

And save the modified data:

protected void WriteYaml<T>(string outputPath, T obj)
{
  var serializer = new SerializerBuilder().Build();
  using (var writer = new StreamWriter(outputPath))
  {
    serializer.Serialize(writer, obj);
  }
}

Take a look at the docker compose file. It now looks like this:

version: 3

services:
  app:
    container_name: MyApp
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
    - ..:/workspaces:cached
    network_mode: service:db
    command: sleep infinity

  db:
    container_name: MyDB
    image: mcr.microsoft.com/mssql/server:2022-latest
    restart: unless-stopped
    environment:
      ACCEPT_EULA: Y
      COLLATION: Latin1_General_CI_AS
      SA_PASSWORD: SomePassword

  anotherservice:
    container_name: AnotherService
    build:
      context: .
      dockerfile: Dockerfile.another
    network_mode: service:db

As you can see, instead of having still a string representation of the version it has been converted to a numeric value. But that is not the wanted behavior because it causes the docker compose to fail because the data type is wrong.

@EdwardCooke
Copy link
Collaborator

EdwardCooke commented Jul 12, 2023

On the serializerbuilder call withquotingnecessarystrings. That should solve it. You may also need to specify the yaml 1.1 spec enum because of the differences between boils for 1.1 vs 1.2.

@NCC1701M NCC1701M closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants