openapi-directory icon indicating copy to clipboard operation
openapi-directory copied to clipboard

YAML type coercion in description documents

Open commonism opened this issue 2 years ago • 2 comments

Due to yaml type coercion certain values get converted to objects.

E.g. timestamps to datetime objects:

import datetime, yaml
yaml.safe_load(str(datetime.datetime.now().date()))
# datetime.date(2022, 6, 10)

Therefore I'd like to have your opinion in having such values in a description document, such as:

https://github.com/APIs-guru/openapi-directory/blob/3b4280061cea37f1faa8ef63a56c26a40702b681/APIs/1password.com/events/1.0.0/openapi.yaml#L176-L179

https://github.com/APIs-guru/openapi-directory/blob/3b4280061cea37f1faa8ef63a56c26a40702b681/APIs/amadeus.com/2.2.0/openapi.yaml#L54-L56

https://github.com/APIs-guru/openapi-directory/blob/3b4280061cea37f1faa8ef63a56c26a40702b681/APIs/api.video/1/openapi.yaml#L76-L84

https://github.com/APIs-guru/openapi-directory/blob/3b4280061cea37f1faa8ef63a56c26a40702b681/APIs/apidapp.com/2019-02-14T164701Z/openapi.yaml#L4-L8

https://github.com/APIs-guru/openapi-directory/blob/3b4280061cea37f1faa8ef63a56c26a40702b681/APIs/apideck.com/accounting/8.32.1/openapi.yaml#L4720-L4722

commonism avatar Jun 10 '22 20:06 commonism

Which YAML parser and version are you using? js-yaml used to have problems handling dates but I thought that had been fixed...

MikeRalphson avatar Aug 26 '22 07:08 MikeRalphson

Python3 - PyYAML 6.0

I do not consider this a problem with the YAML parser, from my understanding the parser does things according to the specification.

To me this is a problem of the description document not forcing this to be a string either enclosing in "" or via !!str.

I consider the dd invalid by having datetime objects where string is expected.

commonism avatar Aug 29 '22 09:08 commonism

Do you know if pyYAML supports YAML 1.2 or 1.1 only?

MikeRalphson avatar Feb 17 '23 09:02 MikeRalphson

1.1

commonism avatar Feb 20 '23 07:02 commonism

These documents are YAML 1.2, as recommended by the OpenAPI Specification:

In order to preserve the ability to round-trip between YAML and JSON formats, YAML version 1.2 is RECOMMENDED

As we basically have no control over the YAML output by our chosen yaml parser, I'm unfortunately going to close this as not-a-bug.

MikeRalphson avatar Feb 20 '23 11:02 MikeRalphson

YAML 1.2 does not change anything here.

As an example, ruamel.yaml does YAML 1.2:

from ruamel.yaml import YAML
yaml = YAML()
yaml.load("example: 2020-06-11T16:32:50-03:00")
# ordereddict([('example', TimeStamp(2020, 6, 11, 19, 32, 50))])

example expects string.

This makes this not a valid description document?

commonism avatar Feb 20 '23 11:02 commonism

From https://yaml.org/spec/1.2.2/ext/changes/

Changes in version 1.2 (revision 1.2.0) (2009-07-21) The !!pairs, !!omap, !!set, !!timestamp and !!binary types have been dropped.

eemeli/yaml only parses timestamp-like strings as Dates if a YAML 1.1 directive is included or !!timestamp is added as a custom type.

MikeRalphson avatar Feb 22 '23 14:02 MikeRalphson

It doesn't help that the YAML 1.2 spec contains three (orphan?) references to !! timestamp without explicitly referencing where it comes from.

MikeRalphson avatar Feb 23 '23 08:02 MikeRalphson

Closing due to inactivity - but please feel free to chase and we can reopen the issue if necessary.

MikeRalphson avatar May 15 '23 16:05 MikeRalphson