mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
701 stars 206 forks source link

Conversion not working right #71

Open dingo-d opened 6 years ago

dingo-d commented 6 years ago

I am trying to convert the yaml file to json, my test file is the one from here, the pet store. The yaml file looks like this:

swagger: "2.0"
info:
  description: "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters."
  version: "1.0.0"
  title: "Swagger Petstore"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
  description: "Everything about your Pets"
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
- name: "store"
  description: "Access to Petstore orders"
- name: "user"
  description: "Operations about user"
  externalDocs:
    description: "Find out more about our store"
    url: "http://swagger.io"
...

The saved json file (from their site) looks like

{
  "swagger": "2.0",
  "info": {
    "description": "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters.",
    "version": "1.0.0",
    "title": "Swagger Petstore",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
      "email": "apiteam@swagger.io"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "host": "petstore.swagger.io",
  "basePath": "/v2",
  "tags": [
    {
      "name": "pet",
      "description": "Everything about your Pets",
      "externalDocs": {
        "description": "Find out more",
        "url": "http://swagger.io"
      }
    },
    {
      "name": "store",
      "description": "Access to Petstore orders"
    },
    {
      "name": "user",
      "description": "Operations about user",
      "externalDocs": {
        "description": "Find out more about our store",
        "url": "http://swagger.io"
      }
    }
  ],
...

But when I try to parse this, the array I get looks like

(
    [swagger] => 2.0
    [info] => Array
        (
            [description] => This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters.
            [version] => 1.0.0
            [title] => Swagger Petstore
            [termsOfService] => http://swagger.io/terms/
            [contact] => Array
                (
                    [email] => apiteam@swagger.io
                )

            [license] => Array
                (
                    [name] => Apache 2.0
                    [url] => http://www.apache.org/licenses/LICENSE-2.0.html
                )

        )

    [host] => petstore.swagger.io
    [basePath] => /v2
    [tags] => 
    [0] => Array
        (
            [name] => pet
            [description] => Everything about your Pets
            [externalDocs] => Array
                (
                    [description] => Find out more
                    [url] => http://swagger.io
                )

        )

    [1] => Array
        (
            [name] => store
            [description] => Access to Petstore orders
        )

    [2] => Array
        (
            [name] => user
            [description] => Operations about user
            [externalDocs] => Array
                (
                    [description] => Find out more about our store
                    [url] => http://swagger.io
                )

        )

    [schemes] => 
    [3] => http
    [paths] => Array
...

Which, when converted to json looks like:

{
      "swagger": "2.0",
      "info": {
        "description": "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters.",
        "version": "1.0.0",
        "title": "Swagger Petstore",
        "termsOfService": "http://swagger.io/terms/",
        "contact": {
          "email": "apiteam@swagger.io"
        },
        "license": {
          "name": "Apache 2.0",
          "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
        }
      },
      "host": "petstore.swagger.io",
      "basePath": "/v2",
      "tags": "",
      "0": {
        "name": "pet",
        "description": "Everything about your Pets",
        "externalDocs": {
          "description": "Find out more",
          "url": "http://swagger.io"
        }
      },
      "1": {
        "name": "store",
        "description": "Access to Petstore orders"
      },
      "2": {
        "name": "user",
        "description": "Operations about user",
        "externalDocs": {
          "description": "Find out more about our store",
          "url": "http://swagger.io"
        }
      },
      "schemes": "",
      "3": "http",
...

So obviously the parser fails when the yaml keys aren't indented

tags:
- name: "pet"

Any fix for this?