aj-foster / open-api-generator

Open API code generator for Elixir
MIT License
97 stars 13 forks source link

(BadMapError) expected a map, got: "string" #52

Closed cspags closed 1 month ago

cspags commented 2 months ago

Hi there, super interested in trying this out, but I'm getting an error for the spec I'm using it with. Any help would be much appreciated, thank you!

Spec: https://github.com/webflow/openapi-spec/blob/main/openapi/v2.yml

Error:

mix api.gen default ./v2.yml

10:50:18.864 [debug] Beginning code generation
Profile: "default"
Files: [
  "./v2.yml"
]

10:50:18.868 [debug] Reading file ./v2.yml
** (BadMapError) expected a map, got: "string"
    (elixir 1.17.1) lib/map.ex:535: Map.get("string", "title", nil)
    (oapi_generator 0.1.1) lib/open_api/spec/schema.ex:152: OpenAPI.Spec.Schema.decode/2
    (oapi_generator 0.1.1) lib/open_api/reader/state.ex:180: OpenAPI.Reader.State.with_schema_ref/3
    (oapi_generator 0.1.1) lib/open_api/reader/state.ex:111: OpenAPI.Reader.State.with_path/4
    (oapi_generator 0.1.1) lib/open_api/spec/schema.ex:292: anonymous fn/2 in OpenAPI.Spec.Schema.decode_properties/2
    (stdlib 6.0) maps.erl:860: :maps.fold_1/4
    (oapi_generator 0.1.1) lib/open_api/reader/state.ex:111: OpenAPI.Reader.State.with_path/4
    (oapi_generator 0.1.1) lib/open_api/spec/schema.ex:143: OpenAPI.Spec.Schema.decode/2

Repo to reproduce: https://github.com/cspags/ex_webflow_api_client

aj-foster commented 2 months ago

Hi @cspags, thanks for reporting this.

I spent some time looking into what's causing this, and the spec has an interesting pattern in it:

- properties:
    code:
      type: string
      description: Error code
      enum:
        - missing_scopes
    type: string

As a human reading this, I interpret it to mean that the parent object has a property code and can have additional properties with any name and string values. Normally in this situation I would see something like this:

- properties:
    code:
      type: string
      description: Error code
      enum:
        - missing_scopes
  additionalProperties:
    type: string

This is done elsewhere in the spec, so it's interesting that they don't do it here. It's also possible that they want to specify a property called type with string values, which would normally look like this:

- properties:
    code:
      type: string
      description: Error code
      enum:
        - missing_scopes
    type:
      type: string

I'm not sure if you have a way to ask questions of the spec authors, but it may be worth reaching out if so. While it's definitely possible for me to modify the reader to look for type: _ as a property and treat it as a generic additional property, this could have the potential side affect of not allowing specs with a property called type.

Of course, it's always possible I've misinterpreted part of the OpenAPI spec. If so, let me know!

cspags commented 1 month ago

Thanks @aj-foster for taking the time to look into this, I appreciate it. I agree it looks like an issue with the spec. I will reach out to them and see what I can learn. Feel free to close this issue as I think you're right that it's not worth accounting for this due to the potential for side effects.

Thank you!

aj-foster commented 1 month ago

Hope you get some resolution. As you learn more about their intent, I'd love to hear about it! ❤️