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

Issue getting inference working

Open tekanic opened this issue 4 years ago • 3 comments

I setup my amber project with openapi-generator but when I try to get inference working I'm getting a build error that has something to do with the helpers...

There was a problem expanding macro 'macro_4654287824'

Code in lib/openapi-generator/src/openapi-generator/helpers/amber.cr:336:5

336 | {% for method_name, content_type in TYPE %} ^ Called macro defined in lib/openapi-generator/src/openapi-generator/helpers/amber.cr:336:5

336 | {% for method_name, content_type in TYPE %}

Which expanded to:

34 | macro json(schema, content_type) 35 | {% hash_ref = ::OpenAPI::Generator::Helpers::Amber::HASH_ITEM_REF[0] %} 36 | {% code = hash_ref[0] %} ^ Error: undefined macro method 'NilLiteral#[]'

tekanic avatar Feb 05 '22 20:02 tekanic

Hi @tekanic,

I setup my amber project with openapi-generator but when I try to get inference working I'm getting a build error that has something to do with the helpers...

I'm sorry but I cannot tell you exactly what is wrong with the information you provided.

Could you describe a bit more how you have setup the library? And in which context you are calling json?

elbywan avatar Feb 06 '22 08:02 elbywan

In my application.cr file I have the following setup:

require "openapi-generator"
require "amber"
require "openapi-generator/providers/amber"

require "./settings"
require "./logger"
require "./i18n"
require "./database"
require "./initializers/**"

# Start Generator Dependencies: Don't modify.
require "../src/models/**"
# End Generator Dependencies

require "../src/controllers/application_controller"
require "../src/controllers/**"
require "./routes"

In my routes.cr file I have the following after the Amber::Server.configure block:

OpenAPI::Generator::Helpers::Amber.bootstrap
OpenAPI::Generator.generate(
  provider: OpenAPI::Generator::RoutesProvider::Amber.new,
  options: {
      output: Path[Dir.current] / "public" / "opeanapi.yml"
    },
      base_document: {
      info: {
      title: "TEST",
      version: "v0.1"
  }
  }
)

In my controllers I require openapi-generator/helpers/amber like so:

require "json"
require "openapi-generator/helpers/amber"

class SurveyController < ApplicationController

  include ::OpenAPI::Generator::Controller
  include ::OpenAPI::Generator::Helpers::Amber

[clipped for brevity] 

Is there something I'm doing incorrectly? Perhaps, I did not understand the documenation.

I am using the latest version of crystal and amber. Thanks for the help!

tekanic avatar Feb 06 '22 13:02 tekanic

In my controllers (index method), I typically build a json response that looks like this...

{
  "widgets":[widget records [50]...],
  "total_records": 1000,
  "offset": 0,
  "limit": 50
}

How would I programmatically create a schema for this type of response?

Is this enough information or is there something specific that I'm missing? Thanks!

tekanic avatar Feb 06 '22 14:02 tekanic