Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

yaml.Unmarshal: Line number in error message when does not seem correct #1055

@micovery

Description

@micovery

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions