Aws-sam-cli: 0.2.5 & 0.2.6 Cannot parse Swagger definition

Created on 24 Jan 2018  路  14Comments  路  Source: aws/aws-sam-cli

  • 0.2.4 sam local start-api works fine

  • 0.2.5 & 0.2.6
    Cannot parse Swagger definition: invalid character '-' in numeric literal

If I remove the --- it will Cannot parse Swagger definition: invalid character 's' looking for beginning of value

Using external swagger file since according to docs in-line is not yet supported.

swagger: 2.0

info:
  title: example
  version: 0.1.0

paths:
  /:
    post:
      x-amazon-apigateway-integration:
        httpMethod: POST
        type: aws_proxy
        uri: arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:<<region>>:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations

Anyone else facing the same issue?

Most helpful comment

I also found that quoting works fine. So swagger: "2.0" worked.

All 14 comments

@Tanbouz, I landed here via google due to the same issue, using the example from https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/swagger.yaml, same errors on 0.2.6.

A json template might be a workaround. https://github.com/awslabs/aws-sam-local/blob/develop/test/templates/open-api/pet-store-simple.json loaded successfully for me during _sam local start-api_.

simple template.yaml excerpt:

...
Description: foo
Resources:
  myAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      DefinitionUri: ./pet-store-simple.json
....

It is external YAML that does not work. External JSON works fine.

Using yaml2json as global, I was able to use prestart npm to convert it. Then use npm start to run sam local start-api using the json as the ref in template.yaml as a work around. Adding the json file to ignore git list so it not commited.

@majway27 @pal-philbasford - I got the same issue after updating to 0.2.6. As you've suggested, using a json swagger file instead of the yaml solved it for me.

Ran into this today as well, switching to json fixed it.

The bug is here:

https://github.com/awslabs/aws-sam-local/blob/dbbd5948fef0b51d412b4e2631e004c7b113c35f/router/api.go#L35-L42

Line 42 assumes that the file loaded is a JSON file.

If the file location is a string, in my case it was 'api.yml', then the first case is met:

https://github.com/awslabs/aws-sam-local/blob/dbbd5948fef0b51d412b4e2631e004c7b113c35f/router/api.go#L158-L163

The URI method only reads the file, regardless of contents:

https://github.com/awslabs/aws-sam-local/blob/dbbd5948fef0b51d412b4e2631e004c7b113c35f/router/api.go#L191-L197

The YAML file text is being saved in the variable 'jsonDefinition', failing at line 42 when it tries to parse the YAML as JSON.

how are you people dealing with variable in the JSON file? For example:

I have this in my template.yml

Type: AWS::Serverless::Api
    Properties:
        DefinitionUri: ./user-api.json
        StageName: Prod
        Variables:
          ServerlessExpressLambdaFunctionName: !Ref UserFunctions
uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:860388958568:function:${stageVariables.ServerlessExpressLambdaFunctionName}/invocations

when used in the JSON version of the swagger spec? I get this error:
Could not extract Lambda function ARN: Could not find match in

I wrote a node app that generates a template.yml and api.json from ejs templates. All of my lambda functions are in a subfolder. Running the app creates an api for all of them when preparing to do a deployment.

Also broken upgrading from 0.2.4 to 0.2.6.

Is this issue planned to be patched? I didn't realize JSON was the only supported Swagger format.

Error I get is:
Cannot parse Swagger definition: json: cannot unmarshal number into Go struct field SwaggerProps.swagger of type string

I got this error as well, removing swagger: 2.0 from the inline yaml resolved it.

I also found that quoting works fine. So swagger: "2.0" worked.

I've had the same experience. Simple workaround, but odd.

this is fixed as of v0.3.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dinvlad picture dinvlad  路  27Comments

burck1 picture burck1  路  45Comments

sanathkr picture sanathkr  路  37Comments

oldnerd picture oldnerd  路  25Comments

alanchavez88 picture alanchavez88  路  53Comments