question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

YAML output missing quotations around examples

See original GitHub issue

When processing a swagger 2 YAML file with a property that is a string with a “date” format and an example provided, the objected is processed properly; however, when output back as an open api 3 YAML file the example object no longer has quotations around it. This causes the wrong example value to display in the UI. When outputting as json this is not a problem as it is a YAML specific issue to require the quotations.

Checklist

  • Conversion: I have checked my source definition is valid OpenAPI 2.0
  • Conversion: On valid OpenAPI 2.0 input, the conversion looks wrong

Detailed Description

In the initial swagger 2.0 I have a property “date range”, which is shown below. In this the example dates “2017-01-01” have quotations around them as required since they are a string example.

Swagger2:

  date_range:
    description: 'DateRange is a type of NumericRange.'
    type: object
    properties:
      from:
        description: 'The lower bound value in the DateRange.'
        type: string
        format: date
        example: "2017-01-01"
      thru:
        description: 'The upper bound value in the DateRange.'
        type: string
        format: date
        example: "2017-01-01"

When outputting this as a YAML open api 3 it is represented as:

Open API 3 YAML

    date_range:
      description: DateRange is a type of NumericRange.
      type: object
      properties:
        from:
          description: The lower bound value in the DateRange.
          type: string
          format: date
          example: 2017-01-01
        thru:
          description: The upper bound value in the DateRange.
          type: string
          format: date
          example: 2017-01-01

This is the exact same minus the quotes around the example. Which causes this to be represented incorrectly in the example viewer:

“from”: {}, “thru”: {}

When converting to JSON, the inherit nature of the JSON shows this correctly based on this conversion:

        "date_range": {
            "description": "DateRange is a type of NumericRange.",
            "type": "object",
            "properties": {
                "from": {
                    "description": "The lower bound value in the DateRange.",
                    "type": "string",
                    "format": "date",
                    "example": "2017-01-01"
                },
                "thru": {
                    "description": "The upper bound value in the DateRange.",
                    "type": "string",
                    "format": "date",
                    "example": "2017-01-01"
                }
            }
        },

Other stuff

Running version 5.2.3. Using the command line to convert using swagger2openapi --yaml --refSiblings preserve --outfile out.yaml in.yaml

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Amachuacommented, Jul 31, 2019

Investigation on the date/date-time format

Also, while investigating on issue opened by @matthewvelie. I’m a bit worried since, if I’m not wrong, the idea of the swagger2openapi tool is to convert a valid Swagger 2.0 to a valid OpenAPI 3.0 and the sample he provided may not be correct regarding the YAML specification but do are working as we expect with the official Swagger tools (see picture below on Swagger editor).

Without quotes

image

With quotes

image

As for my previous comment, you’ll find

  • random.txt: with the specification in Swagger 2.0;
  • random.openapi.txt: With the specification in OpenAPI 3.0 that was generated with the command line swagger2openapi.ps1 -targetPath .\publish .\test\random.yaml and that you can check on Swagger Editor to see the issue I’ve shared with you.

With this new information, do you think you could make an update on your tool or should we ask for an update of the Swagger tools? 😃

random.txt random.openapi.txt

1reaction
Amachuacommented, Jul 31, 2019

String number conversion issue

I’ve a related issue while converting a file from Swagger 2.0 to OpenAPI 3.0 but its not for the date nor the date-time but on the string that enclose a number.

To make you understand the issue, I’ve attached two sample files:

  • random.txt: with the specification in Swagger 2.0;
  • random.openapi.txt: With the specification in OpenAPI 3.0 that was generated with the command line swagger2openapi.ps1 -targetPath .\publish .\test\random.yaml.

In those files, you’ll see the following behavior. A string that starts with a ‘0’ and that only contains number in it will be automatically converted to a number and lose its quotes like below:

Definition from random.txt

  RandomRequest:
    type: object
    properties:
      random_phone_number:
        description: Some string number.
        type: string
        example: "0681924019"
      other_random_phone_number:
        description: Some string number.
        type: string
        example: "681924019"
    required: [random_phone_number]

Converted definition in random.openapi.txt

    RandomRequest:
      type: object
      properties:
        random_phone_number:
          description: Some string number.
          type: string
          example: 681924019
        other_random_phone_number:
          description: Some string number.
          type: string
          example: "681924019"
      required:
        - random_phone_number

This is particularly painful since the generated sample is not valid without the quote. Do you have any idea on how to solve this? 😃

random.openapi.txt random.txt

Read more comments on GitHub >

github_iconTop Results From Across the Web

YAML: Do I need quotes for strings in YAML?
In general, you don't need quotes. Use quotes to force a string, e.g. if your key or value is 10 but you want...
Read more >
Avoid YAML formatting problems - Auditbeat Reference
To prevent unwanted type conversions, avoid using leading zeros in field values, or wrap the values in single quotation marks. Avoid accidental template ......
Read more >
Strings in YAML - To Quote or not to Quote | tinita [blogs.perl.org]
In YAML, you can write a string without quotes, if it doesn't have a special meaning. See the next section for cases where...
Read more >
PyYAML Documentation
PyYAML is a YAML parser and emitter for Python. Installation. Simple install: pip install pyyaml. To install from source, download the source package...
Read more >
YAML: The Missing Battery in Python
In this tutorial, you'll learn all about working with YAML in Python ... The all-around YAML serialization function in PyYAML is yaml.dump() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found