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

YAML spec violation: Duplicate keys are silently ignored, and parser selects incorrect key #154

Closed
RichiH opened this issue Dec 28, 2015 · 7 comments

Comments

@RichiH
Copy link

RichiH commented Dec 28, 2015

As per prometheus/prometheus#1275 :

If you run with a configuration like:

scrape_configs: <<<<<<<<<<<<<< this is discarded
  - job_name: 'prometheus_system'
    target_groups:
    - targets: ['localhost:9100']

scrape_configs:
  - job_name: 'foo_system'
    target_groups:
    - targets: ['foot:9100']

the former scrape_configs is silently discarded. While that's the obvious case, constructs like

scrape_configs: <<<<<<<<<<<<<< this is discarded
  - job_name: 'prometheus_system'
    target_groups:
    - targets: ['localhost:9100']

rule_files:
  - 'prometheus.rules'

scrape_configs:
  - job_name: 'foo_system'
    target_groups:
    - targets: ['foo:9100']

are silently discarded as well.

Raising an error would be appreciated, and suggested by the YAML specs, in this situation.

Also, as @fabxc notes, http://yaml.org/spec/1.1/#id932806 says that the first, not the last, occurence should be valid.

@davidje13
Copy link

We have created a new pull request to resolve this; it includes tests for the situation described, and tests for valid cases (e.g. duplicate values are OK; only check for duplicate keys)

@stub42
Copy link

stub42 commented Sep 11, 2017

Can this land on the v2 branch, or does this fix break backwards compatibility and need to land on a v3 branch?

@jsha
Copy link

jsha commented Feb 8, 2018

Any update on possibly merging to the v2 branch?

@4a6f656c
Copy link

4a6f656c commented Mar 9, 2018

I believe this is addressed in the v2 branch via UnmarshalStrict. Although be aware that this also requires that there are fields present in the struct for all fields in the YAML data.

@rogpeppe - this should probably have been tagged as fixed in #307 - unless there are plans to add duplicate key detection to non-strict mode, then I suspect this should be closed.

@niemeyer
Copy link
Contributor

I will review this behavior and perhaps make it standard in v3. But yet, today that new strict mode already handles the case discussed here.

@davidje13
Copy link

davidje13 commented Mar 26, 2018

@niemeyer in strict mode yes this sounds resolved, but if the non-strict mode doesn't error, I'd say it should only be considered resolved once it follows the spec (as mentioned in the original issue)

It is an error for two equal keys to appear in the same mapping node. In such a case the YAML processor may continue, ignoring the second key: value pair and issuing an appropriate warning. This strategy preserves a consistent information model for one-pass and random access applications.

http://yaml.org/spec/1.1/#id932806

i.e. the first not the last entry should be the one which is kept.

Edit: actually the latest (1.2) spec just says:

The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique
[…]
JSON's RFC4627 requires that mappings keys merely “SHOULD” be unique, while YAML insists they “MUST” be.

With no suggestion of gracefully handling conflicting keys in any way, I'm inclined to say that for YAML 1.2 compliance, rejecting duplicate keys should be standard in both strict and non-strict modes.

@niemeyer
Copy link
Contributor

The 1.2 spec actually says that keys are unique, period. So any changes in behavior at this point simply means a choice of which way to handle a broken document, and we're not going to change the way we handle broken documents now because that can break actual software that is working today. For that reason, v2 won't change.

I will review the behavior before considering v3 final, but I'm not yet making any promises on the direction we'll go there either.

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

6 participants