decorators-squad / eo-yaml

YAML for Java 8 and above. A user-friendly OOP library. Previously known as "Camel".
BSD 3-Clause "New" or "Revised" License
261 stars 52 forks source link

YamlIndentationException: Indentation of line 4 is greater than the one of line 3 #340

Closed Sammers21 closed 4 years ago

Sammers21 commented 4 years ago

Some of the YAML files, which this project is not able to parse.

apiVersion: v1
appVersion: 5.7.30
description: Fast, reliable, scalable, and easy to use open-source relational database
  system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
maintainers:
- email: o.with@sportradar.com
  name: olemarkus
- email: viglesias@google.com
  name: viglesiasce
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.4
apiVersion: v1
name: tomcat
description: Deploy a basic tomcat application server with sidecar as web archive container
icon: http://tomcat.apache.org/res/images/tomcat.png
version: 0.4.1
appVersion: "7.0"
home: https://github.com/yahavb
maintainers:
  - name: yahavb
    email: ybiran@ananware.systems

Stack trace:

com.amihaiemil.eoyaml.exceptions.YamlIndentationException: Indentation of line 4 is greater than the one of line 3. It should be less or equal.

    at com.amihaiemil.eoyaml.WellIndented.iterator(WellIndented.java:120)
    at com.amihaiemil.eoyaml.SameIndentationLevel.iterator(SameIndentationLevel.java:74)
    at com.amihaiemil.eoyaml.ReadYamlMapping.valueOfStringKey(ReadYamlMapping.java:182)
    at com.amihaiemil.eoyaml.ReadYamlMapping.value(ReadYamlMapping.java:136)
    at com.amihaiemil.eoyaml.YamlMapping.string(YamlMapping.java:147)
    at com.amihaiemil.eoyaml.YamlMapping.string(YamlMapping.java:135)
    at com.artipie.helm.TgzArchive.name(TgzArchive.java:82)
    at com.artipie.helm.TarArchiveTest.nameIdentifiedCorrectly(TarArchiveTest.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Note: some of the YAML validators claims that those 2 files are actually valid ones

Sammers21 commented 4 years ago

@amihaiemil, Is there any option to remove the validation part or make it less strict?

amihaiemil commented 4 years ago

@Sammers21 No, unfortunately validation cannot be disabled or removed. The YAML format has some strict rules which have to be followed and indentation is one of them -- that's why we try to validate that.

There is a known bug in the eo-yaml, and that is sequences or mappings within a sequence have to start at the next line with reference to the -.

The following are both valid YAML Sequences:

- scalar1
- key: value
  key1: value1
- scalar2
- scalar1
-
  key: value
  key1: value1
- scalar2

But eo-yaml can only parse the second version correctly. I see in your examples there are mappings as sequence elements which start on the same line as the -. E.g.:

- name: yahavb
  email: ybiran@ananware.systems

Should be:

-
  name: yahavb
  email: ybiran@ananware.systems

Can you make this change and try again? There are also other issues in your YAML, which are real indentation problems. For instance:

keywords:
- mysql
- database
- sql

is not correctly indented. It should be:

keywords:
  - mysql
  - database
  - sql
amihaiemil commented 4 years ago

@Sammers21 Here is your first YAML, which should work now (I fixed it):

apiVersion: v1
appVersion: 5.7.30
description: Fast, reliable, scalable, and easy to use open-source relational database system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
  - mysql
  - database
  - sql
maintainers:
  -
    email: o.with@sportradar.com
    name: olemarkus
  -
    email: viglesias@google.com
    name: viglesiasce
name: mysql
sources:
  - https://github.com/kubernetes/charts
  - https://github.com/docker-library/mysql
version: 1.6.4

and the second one:

apiVersion: v1
name: tomcat
description: Deploy a basic tomcat application server with sidecar as web archive container
icon: http://tomcat.apache.org/res/images/tomcat.png
version: 0.4.1
appVersion: "7.0"
home: https://github.com/yahavb
maintainers:
  -
    name: yahavb
    email: ybiran@ananware.systems
Sammers21 commented 4 years ago

@amihaiemil, I'm not writing those YAML files and that is why I could not correct them. Some of the example you can find here: https://github.com/helm/charts/tree/master/stable

amihaiemil commented 4 years ago

@Sammers21 well, even if the bug in eo-yaml was not there, they are still not correct because of the keywords and sources elements indentation...

I cannot make any changes about that because YAML indentation is really relevant and it has to be correct.

amihaiemil commented 4 years ago

@Sammers21 And particularly, in eo-yaml, indentation is used in the reading logic. So even if I take out the validation, it will not work because eo-yaml will not read the elements correctly.

Sammers21 commented 4 years ago

@amihaiemil, why all the Yaml validators are saying the opposite then? And can you refer to a specific spec paragraph?

Sammers21 commented 4 years ago

@amihaiemil, also, is there any way to fix the formatting without any manual manipulations?

amihaiemil commented 4 years ago

@Sammers21 See here. The Specification defines that structure is determined by indentation in Block YAML.

why all the Yaml validators are saying the opposite then?

I don't know, but you should know that people generally avoid YAML particularly because the Specification is too complex and there are very few YAML parsers that do not have any problems.

is there any way to fix the formatting without any manual manipulations?

No, eo-yaml just Reads the YAML. If inconsistencies are found in indentation/structure, it cannot really know what the correct path is so it throws an exception.

Sammers21 commented 4 years ago

@amihaiemil, as a note: snakeyaml works perfectly fine with those files.

amihaiemil commented 4 years ago

@Sammers21 snakeyaml is much older and robust than eo-yaml :)

amihaiemil commented 4 years ago

@Sammers21 I'm closing this since there's nothing I can do about it.

portlek commented 4 years ago

a little note, its called MapList in yaml/json. in json it looks like that;

{
  "test": "test",
  "test-map-list": [
    {
      "email": "o.with@sportradar.com",
      "name": "olemarkus"
    },
    {
      "email": "viglesias@google.com",
      "name": "viglesiasce"
    },
  ],
  "test-2": "test-2"
}
Sammers21 commented 4 years ago

@amihaiemil, why this issue is open again?

amihaiemil commented 4 years ago

@Sammers21 Because I'm working on it. First step is to fix the problem with mappings within a sequence, that start at the same line with the dash (-). This is almost done.

Then, I'll have a look and see if I can do something about the indentation issues... maybe I can add some formatting rules.

Sammers21 commented 4 years ago

@amihaiemil, what do you thing about wrapping snake-yaml, while keeping the design without any changes?

amihaiemil commented 4 years ago

@Sammers21 That's not an option, sorry. A library should fix its own problems and requirements, not wrap another library in yet another set of abstractions.

amihaiemil commented 4 years ago

@Sammers21 but if you like eo-yaml's interfaces, you can implement them yourself, based on snake-yaml. It shouldn't be hard :)

amihaiemil commented 4 years ago

@Sammers21 The bug about reading mappings within sequences, which start at the dash line, has just been fixed and released with version 5.0.3. See here.

I'll continue with #361 and see if I can implement some default formatting rules.

amihaiemil commented 4 years ago

@Sammers21 @g4s8 I just released version 5.1.0 which allows you to bypass the validation of indentation.

However, this might result in a different YAML than what you expect, so I only recommend you do it if you have proper tests for your code. More details here.

I'm closing this Issue now as I think all the related bugs have been fixed. Please feel free to open another one if you still have problems.