aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

0.2.5 & 0.2.6 Cannot parse Swagger definition

Open Tanbouz opened this issue 8 years ago • 14 comments

  • 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?

Tanbouz avatar Jan 24 '18 15:01 Tanbouz

@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
....

majway27 avatar Jan 24 '18 22:01 majway27

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

pal-philbasford avatar Jan 26 '18 23:01 pal-philbasford

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.

pal-philbasford avatar Jan 27 '18 20:01 pal-philbasford

@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.

rbbhar avatar Jan 29 '18 21:01 rbbhar

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

ldm314 avatar Jan 30 '18 04:01 ldm314

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.

ldm314 avatar Jan 30 '18 06:01 ldm314

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.

ldm314 avatar Jan 30 '18 07:01 ldm314

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

aaronj314 avatar Jan 30 '18 19:01 aaronj314

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.

ldm314 avatar Jan 31 '18 20:01 ldm314

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

jhovell avatar Feb 02 '18 22:02 jhovell

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

wolfeidau avatar Feb 16 '18 23:02 wolfeidau

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

metaskills avatar Feb 16 '18 23:02 metaskills

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

michaelj-smith avatar Feb 17 '18 01:02 michaelj-smith

this is fixed as of v0.3.0

sanathkr avatar Jun 01 '18 00:06 sanathkr