Coder Social home page Coder Social logo

YAML OpenAPI format/profile about yaml-pp-p5 HOT 5 OPEN

pali avatar pali commented on August 25, 2024
YAML OpenAPI format/profile

from yaml-pp-p5.

Comments (5)

perlpunk avatar perlpunk commented on August 25, 2024

I worked a bit with OpenAPI, but I'm not uptodate.

That said,

OpenAPI specification is at website https://swagger.io/specification/ and says that it uses YAML 1.2 format with some constrains:

Yes, YAML::PP implements all 3 schemas from 1.2 (Failsafe, JSON and Core).
However, the JSON schema diverges from the official one, in that strings in gerneral don't have to be quoted (unless special values).
If I read the specification, it actually says

null                                                           | tag:yaml.org,2002:null
true \| false                                                  | tag:yaml.org,2002:bool
-? ( 0 \| [1-9] [0-9]* )                                       | tag:yaml.org,2002:int
-? ( 0 \| [1-9] [0-9]* ) ( \. [0-9]* )? ( [eE] [-+]? [0-9]+ )? | tag:yaml.org,2002:float
*                                                              | Error

So anything that is not null, boolean or a number, needs to be quoted.
I plan to add this original Schema, maybe as JSONStrict or something.
From what I understand, OpenAPI does not want anyone to quote all strings, so they probably also mean the less stricter version of it.

To use the JSON Schema:
my $yp = YAML::PP->new( schema => ['JSON'] );

This is actually the current default, but I decided that it was not a good decision. The Core schema should be the default, because that will be compatible to most other YAML processors that implement 1.2. Some of them only implement the Core Schema. (I want to change it and hope I can do that soon and not too much users really depend on the default right now...)

This is not something you can restrict YAML::PP to without being prepared for changes. You can already add some callbacks to influence the loading process, but it's still in the works, and overriding the hash construction is probably something that will change.

And could be YAML::PP configured to generate YAML file from Perl structure according to these constrains?

You mean dumping an OpenAPI specification to YAML? Only with the first constraint currently.
Edit: Well, actually since in perl all keys are strings, with both constraints can be made for dumping.

I'm always happy to hear about new use cases. So far only one CPAN project I know of is using the tag resulution/callback API.

from yaml-pp-p5.

perlpunk avatar perlpunk commented on August 25, 2024

Another comment about this: The Failsafe schema does not restrict mapping keys to only scalars.

YAML places no restrictions on the type of keys; in particular, they are not restricted to being scalars.

So I find this rule a bit confusing.

from yaml-pp-p5.

pali avatar pali commented on August 25, 2024

Primary use case is:
Load & convert OpenAPI 3 YAML file correctly to Perl in-memory structure (e.g. hashref/arrayref).

We have already find out that e.g. Python YAML parser has problems with yes and no keys. According to OpenAPI 3, following YAML file

somekey:
 - yes
 - value

should be interpreted as somekey => [ 'yes', 'value' ] (both yes and value as strings). But e.g. python interprets unquoted yes as boolean value, not as string. And unquoted false should be boolean false value.

I do not know if there can be other problems, but booleans and unquoted literals are the first thing which started causing problems in more languages when we were trying to load OpenAPI 3 YAML files.

from yaml-pp-p5.

perlpunk avatar perlpunk commented on August 25, 2024

Yes, Python's PyYAML implements only the YAML 1.1 types, so it cannot be used for this. You can try the alternative ruamel.yaml.

Besides booleans and null values, also the regular expressions for numbers are different between the YAML 1.2 Core and JSON schema.
So by explicitly using the JSON schema in YAML::PP you should be safe.
You probably also want to enable JSON::PP booleans:
my $yp = YAML::PP->new( schema => ['JSON'], boolean => 'JSON::PP' );

from yaml-pp-p5.

pali avatar pali commented on August 25, 2024

So by explicitly using the JSON schema in YAML::PP you should be safe.

Ok, thank you very much for information.

from yaml-pp-p5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.