oxidecomputer / typify

compiler from JSON Schema into idiomatic Rust types
Apache License 2.0
446 stars 59 forks source link

Cannot parse the openapi specification itself #523

Open kaspermarstal opened 8 months ago

kaspermarstal commented 8 months ago

Hi, trying to parse the OpenAPI v3.1 specification itself fails with the following error message:

cargo typify ./openapi31.json
The application panicked (crashed).
Message:  not yet implemented: invalid (or unexpected) schema:
{
  "type": "object",
  "additionalProperties": {
    "$ref": "#/$defs/path-item-or-reference"
  },
  "$ref": "#/$defs/specification-extensions",
  "$comment": "https://spec.openapis.org/oas/v3.1.0#callback-object"
}
Location: /home/kasper/.cargo/registry/src/index.crates.io-6f17d22bba15001f/typify-impl-0.0.16/src/convert.rs:700

This library seems awesome. I had hoped to able to use it to work with the openapi spec in code. I don't know much about the details of the above error message however. Is this like a single bug (i.e. it should work) or is this a very complicated file (i.e. this is far off and not expected to work at the moment)?

ahl commented 8 months ago

This looks to be at least a couple of bug fixes away. Proximately (i.e. the bug you hit) is that typify (incorrectly) assumes that references are going to be on their own without other data. This was true in earlier JSON Schema revisions, but is not true in more recent revisions. I think we could do a transformation effectively into this:

{
  "type": "object",
  "additionalProperties": {
    "$ref": "#/$defs/path-item-or-reference"
  },
  "allOf": [
    {
      "$ref": "#/$defs/specification-extensions"
    }
  ],
  "$comment": "https://spec.openapis.org/oas/v3.1.0#callback-object"
}

After that, I see some use of patternProperties that I don't think we're going to handle properly now (#522).

If you're thinking about making a OpenAPI 3.1 crate in the style of openapiv3 I'd be interested in hearing your thoughts. I have elaborate plans around 3.1 support... but the complexities of JSON Schema 2022-10-07 are not insignificant.

kaspermarstal commented 8 months ago

My use-case is auto-generating PostgreSQL FDWs from OpenAPI specs. Is there anything in particular you want to me elaborate on? I am happy to share my thoughts although I know more about PostgreSQL than OpenAPI and JSON schemas.