Sha256: a67cb1dfc2680d4f00998273338f3ffbda4da16f86161b17bf1997728c14a76e

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

class Api::ModelParser < GrapeSwagger::Jsonapi::Parser
  attr_reader :model, :endpoint

  alias_method :schema, :call

  def initialize(model, endpoint)
    @model = model
    @endpoint = endpoint
  end

  def call
    # first let's grab the schema generated by the JSON:API parser
    schema_json = schema.to_json

    # From Nick Schneble:

    # we can easily override these types for our API endpoints in the documentation
    # but we can't do the same thing for the relationship objects that are auto-generated
    # thus the fancy affair below

    # if you want to learn more about what's happening here, read these:
    # https://stackoverflow.com/a/17918118/1322386
    # https://swagger.io/docs/specification/data-models/data-types/

    # Swagger 3.0 only supports a subset of Ruby data types
    schema_json.gsub!("\"type\":\"binary\"", "\"type\":\"string\", \"format\":\"binary\"")
    schema_json.gsub!("\"type\":\"date\"", "\"type\":\"string\", \"format\":\"date\"")
    schema_json.gsub!("\"type\":\"datetime\"", "\"type\":\"string\", \"format\":\"date-time\"")
    schema_json.gsub!("\"type\":\"decimal\"", "\"type\":\"number\", \"format\":\"double\"")
    schema_json.gsub!("\"type\":\"float\"", "\"type\":\"number\", \"format\":\"float\"")
    schema_json.gsub!("\"type\":\"bigint\"", "\"type\":\"integer\", \"format\":\"int64\"")
    schema_json.gsub!("\"type\":\"primary_key\"", "\"type\":\"integer\", \"format\":\"int64\"")
    schema_json.gsub!("\"type\":\"references\"", "\"type\":\"object\"")
    schema_json.gsub!("\"type\":\"text\"", "\"type\":\"string\"")
    schema_json.gsub!("\"type\":\"time\"", "\"type\":\"string\", \"format\":\"time\"")
    schema_json.gsub!("\"type\":\"timestamp\"", "\"type\":\"string\", \"format\":\"timestamp\"")
    schema_json.gsub!("\"type\":\"json\"", "\"type\":\"array\", \"items\":{\"type\":\"string\"}")

    # returns a Hash as if nothing fancy happened
    JSON.parse(schema_json)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bullet_train-api-1.0.17 app/controllers/api/model_parser.rb
bullet_train-api-1.0.16 app/controllers/api/model_parser.rb
bullet_train-api-1.0.15 app/controllers/api/model_parser.rb
bullet_train-api-1.0.14 app/controllers/api/model_parser.rb
bullet_train-api-1.0.13 app/controllers/api/model_parser.rb