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.Unmarshal: Line number in error message when does not seem correct #1055

Open
micovery opened this issue Oct 25, 2024 · 1 comment
Open

Comments

@micovery
Copy link

The program below outputs:

panic: yaml: line 12: did not find expected key

goroutine 1 [running]:
main.main()
        main.go:67 +0x70

Process finished with the exit code 2

It says there is an error on line number 12 ... which does not make sense given the input YAML.

package main

import (
	"gopkg.in/yaml.v3"
)

var input string = `
#  Copyright 2024 Google LLC
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#       http:#www.apache.org/licenses/LICENSE-2.0
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

APIProxy:
  .revision: 1
  .name: swagger-petstore-openapi-3-0
  DisplayName: Swagger Petstore - OpenAPI 3.0
  Description: |- 
    
    This is a sample Pet Store Server based on the OpenAPI 3.0 specification.  You can find out more about
    Swagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
    You can now help us improve the API whether it's by making changes to the definition itself or to the code.
    That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
    
    Some useful links:
    - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
    - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
Policies:
  #  Copyright 2024 Google LLC
  #  Licensed under the Apache License, Version 2.0 (the "License");
  #  you may not use this file except in compliance with the License.
  #  You may obtain a copy of the License at
  #       http:#www.apache.org/licenses/LICENSE-2.0
  #  Unless required by applicable law or agreed to in writing, software
  #  distributed under the License is distributed on an "AS IS" BASIS,
  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  #  See the License for the specific language governing permissions and
  #  limitations under the License.
  
  #- SpikeArrest:
      .continueOnError: false
      .enabled: true
      .name: Spike-Arrest-1
      DisplayName: Spike Arrest
      Properties: {}
      Identifier:
        .ref: client.ip
      Rate: 100pm
  - OASValidation:
      .continueOnError: false
      .enabled: true
      .name: OAS-Validate
      DisplayName: OAS-Validate
      Source: request
      OASResource: oas://spec.yaml

`

func main() {
	yamlNode := yaml.Node{}
	if err := yaml.Unmarshal([]byte(input), &yamlNode); err != nil {
		panic(err)
	}
}

Other tools I've tried (like yamllint.com) correctly identify the error as being on line 47

image
@jackwhelpton
Copy link

I have this same problem, manifesting slightly differently... when using folding characters, the line numbers identified from the parser also don't correlate with the original file. For example:

28  description: |
29     Some field description

Accessing the yaml.Node.Line property while parsing the description returns 28.

This is a rather large problem for us, as I'm trying to write parsing logic that extracts segments from a YAML file for separate analysis and is capable of pointing back to their original location.

If this were to be fixed, I suspect we would need .RawLine and .RawColumn properties so as not to break any existing implementations, with analogous properties on the error.

Given the last release of this package was over two years ago, is it still actively maintained? What are the chances of being able to address this? Unfortunately other options for handling YAML in Go appear to be limited.

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