Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/anchor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Config
:use_type_as_schema_name,
:maybe_as_union,
:array_bracket_notation,
:infer_default_as_non_null
:infer_default_as_non_null,
:ar_comment_to_string

def initialize
@ar_column_to_type = nil
Expand All @@ -22,6 +23,7 @@ def initialize
@maybe_as_union = nil
@array_bracket_notation = nil
@infer_default_as_non_null = nil
@ar_comment_to_string = nil
end
end
end
7 changes: 6 additions & 1 deletion lib/anchor/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def anchor_attributes_properties(included_fields:)
column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
if column
type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
description ||= column.comment if Anchor.config.use_active_record_comment
unless description
description = column.comment if Anchor.config.use_active_record_comment
if description && !Anchor.config.ar_comment_to_string.nil?
description = Anchor.config.ar_comment_to_string.call(description)
end
end
check_presence = type.is_a?(Anchor::Types::Maybe) && Anchor.config.use_active_record_validations
if check_presence && _model_class.validators_on(model_method).any? do |v|
if v.is_a?(ActiveRecord::Validations::NumericalityValidator)
Expand Down
1 change: 1 addition & 0 deletions spec/example/app/resources/exhaustive_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class AssertedObject < Types::Object
attribute :model_overridden
attribute :resource_overridden
attribute :with_comment
attribute :with_parsed_comment
attribute :defaulted_boolean
attribute :defaulted_at

Expand Down
8 changes: 8 additions & 0 deletions spec/example/config/initializers/anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ module Anchor
return Types::Literal.new("never") if column.name == "loljk"
Types::Inference::ActiveRecord::SQL.default_ar_column_to_type(column)
}
c.ar_comment_to_string = lambda { |comment|
begin
res = JSON.parse(comment)
res["description"]
rescue JSON::ParserError
comment
end
}

c.empty_relationship_type = -> { Anchor::Types::Object }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddWithParsedCommentToExhaustives < ActiveRecord::Migration[8.0]
def change
comment = <<~JSON
{
"description": "This is a parsed JSON comment.",
"test": 2
}
JSON
add_column :exhaustives, :with_parsed_comment, :string, comment:
end
end
3 changes: 2 additions & 1 deletion spec/example/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions spec/example/test/files/all_fields_false_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
/** This is a parsed JSON comment. */
withParsedComment: Maybe<string>;
defaultedBoolean: boolean;
defaultedAt: string;
relationships: {};
Expand Down
2 changes: 2 additions & 0 deletions spec/example/test/files/excluded_fields_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
/** This is a parsed JSON comment. */
withParsedComment: Maybe<string>;
defaultedBoolean: boolean;
defaultedAt: string;
relationships: {};
Expand Down
2 changes: 1 addition & 1 deletion spec/example/test/files/json_schema.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"$schema":"https://json-schema.org/draft-07/schema","title":"Schema","type":"object","properties":{"comment":{"$ref":"#/$defs/Comment"},"user":{"$ref":"#/$defs/User"},"post":{"$ref":"#/$defs/Post"},"exhaustive":{"$ref":"#/$defs/Exhaustive"}},"required":["comment","user","post","exhaustive"],"additionalProperties":false,"$defs":{"Comment":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["comments"]},"text":{"type":"string"},"createdAt":{"type":"string"},"updatedAt":{"type":"string"},"relationships":{"type":"object","properties":{"user":{"$ref":"#/$defs/User"},"deletedBy":{"$ref":"#/$defs/User"},"commentable":{"oneOf":[{"$ref":"#/$defs/Post"}]}},"required":["user"],"additionalProperties":false}},"required":["id","type","text","createdAt","updatedAt","relationships"],"additionalProperties":false},"User":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["users"]},"name":{"type":"string"},"role":{"enum":[{"value":"admin"},{"value":"content_creator"},{"value":"external"},{"value":"guest"},{"value":"system"}]},"relationships":{"type":"object","properties":{"comments":{"type":"array","items":{"$ref":"#/$defs/Comment"}},"posts":{"type":"array","items":{"$ref":"#/$defs/Post"}}},"required":["comments","posts"],"additionalProperties":false}},"required":["id","type","name","role","relationships"],"additionalProperties":false},"Post":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["posts"]},"description":{"type":"string"},"relationships":{"type":"object","properties":{"user":{"$ref":"#/$defs/User"},"comments":{"type":"array","items":{"$ref":"#/$defs/Comment"}},"participants":{"type":"array","items":{"$ref":"#/$defs/User"}}},"required":["user","comments","participants"],"additionalProperties":false}},"required":["id","type","description","relationships"],"additionalProperties":false},"Exhaustive":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["exhaustives"]},"assertedString":{"type":"string"},"assertedNumber":{"type":"number"},"assertedBoolean":{"type":"boolean"},"assertedNull":{"type":"null"},"assertedUnknown":{},"assertedObject":{"type":"object","properties":{"a":{"enum":["a"]},"b-dash":{"enum":[1]},"c":{"oneOf":[{"type":"string"},{"type":"null"}]},"d_optional":{"oneOf":[{"type":"string"},{"type":"null"}]}},"required":["a","b-dash","c"],"additionalProperties":false},"assertedMaybeObject":{"oneOf":[{"type":"object","properties":{"a":{"enum":["a"]},"b-dash":{"enum":[1]},"c":{"oneOf":[{"type":"string"},{"type":"null"}]},"d_optional":{"oneOf":[{"type":"string"},{"type":"null"}]}},"required":["a","b-dash","c"],"additionalProperties":false},{"type":"null"}]},"assertedArrayRecord":{"type":"array","items":{"type":"object","additionalProperties":"true"}},"assertedUnion":{"oneOf":[{"type":"string"},{"type":"number"}]},"assertedUnionArray":{"type":"array","items":{"oneOf":[{"type":"string"},{"type":"number"}]}},"withDescription":{"type":"string"},"inferredUnknown":{},"uuid":{"type":"string"},"string":{"type":"string"},"maybeString":{"type":"string"},"text":{"type":"string"},"integer":{"type":"number"},"float":{"type":"number"},"decimal":{"type":"string"},"datetime":{"type":"string"},"timestamp":{"type":"string"},"time":{"type":"string"},"date":{"type":"string"},"boolean":{"type":"boolean"},"arrayString":{"type":"array","items":{"type":"string"}},"maybeArrayString":{"oneOf":[{"type":"array","items":{"type":"string"}},{"type":"null"}]},"json":{"type":"object","additionalProperties":"true"},"jsonb":{"type":"object","additionalProperties":"true"},"daterange":{},"enum":{},"virtualUpcasedString":{"oneOf":[{"type":"string"},{"type":"null"}]},"loljk":{"enum":["never"]},"delegatedMaybeString":{"type":"string"},"modelOverridden":{},"resourceOverridden":{},"withComment":{"oneOf":[{"type":"string"},{"type":"null"}]},"defaultedBoolean":{"type":"boolean"},"defaultedAt":{"type":"string"}},"required":["id","type","assertedString","assertedNumber","assertedBoolean","assertedNull","assertedUnknown","assertedObject","assertedMaybeObject","assertedArrayRecord","assertedUnion","assertedUnionArray","withDescription","inferredUnknown","uuid","string","maybeString","text","integer","float","decimal","datetime","timestamp","time","date","boolean","arrayString","maybeArrayString","json","jsonb","daterange","enum","virtualUpcasedString","loljk","delegatedMaybeString","modelOverridden","resourceOverridden","withComment","defaultedBoolean","defaultedAt"],"additionalProperties":false}}}
{"$schema":"https://json-schema.org/draft-07/schema","title":"Schema","type":"object","properties":{"comment":{"$ref":"#/$defs/Comment"},"user":{"$ref":"#/$defs/User"},"post":{"$ref":"#/$defs/Post"},"exhaustive":{"$ref":"#/$defs/Exhaustive"}},"required":["comment","user","post","exhaustive"],"additionalProperties":false,"$defs":{"Comment":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["comments"]},"text":{"type":"string"},"createdAt":{"type":"string"},"updatedAt":{"type":"string"},"relationships":{"type":"object","properties":{"user":{"$ref":"#/$defs/User"},"deletedBy":{"$ref":"#/$defs/User"},"commentable":{"oneOf":[{"$ref":"#/$defs/Post"}]}},"required":["user"],"additionalProperties":false}},"required":["id","type","text","createdAt","updatedAt","relationships"],"additionalProperties":false},"User":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["users"]},"name":{"type":"string"},"role":{"enum":[{"value":"admin"},{"value":"content_creator"},{"value":"external"},{"value":"guest"},{"value":"system"}]},"relationships":{"type":"object","properties":{"comments":{"type":"array","items":{"$ref":"#/$defs/Comment"}},"posts":{"type":"array","items":{"$ref":"#/$defs/Post"}}},"required":["comments","posts"],"additionalProperties":false}},"required":["id","type","name","role","relationships"],"additionalProperties":false},"Post":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["posts"]},"description":{"type":"string"},"relationships":{"type":"object","properties":{"user":{"$ref":"#/$defs/User"},"comments":{"type":"array","items":{"$ref":"#/$defs/Comment"}},"participants":{"type":"array","items":{"$ref":"#/$defs/User"}}},"required":["user","comments","participants"],"additionalProperties":false}},"required":["id","type","description","relationships"],"additionalProperties":false},"Exhaustive":{"type":"object","properties":{"id":{"type":"number"},"type":{"enum":["exhaustives"]},"assertedString":{"type":"string"},"assertedNumber":{"type":"number"},"assertedBoolean":{"type":"boolean"},"assertedNull":{"type":"null"},"assertedUnknown":{},"assertedObject":{"type":"object","properties":{"a":{"enum":["a"]},"b-dash":{"enum":[1]},"c":{"oneOf":[{"type":"string"},{"type":"null"}]},"d_optional":{"oneOf":[{"type":"string"},{"type":"null"}]}},"required":["a","b-dash","c"],"additionalProperties":false},"assertedMaybeObject":{"oneOf":[{"type":"object","properties":{"a":{"enum":["a"]},"b-dash":{"enum":[1]},"c":{"oneOf":[{"type":"string"},{"type":"null"}]},"d_optional":{"oneOf":[{"type":"string"},{"type":"null"}]}},"required":["a","b-dash","c"],"additionalProperties":false},{"type":"null"}]},"assertedArrayRecord":{"type":"array","items":{"type":"object","additionalProperties":"true"}},"assertedUnion":{"oneOf":[{"type":"string"},{"type":"number"}]},"assertedUnionArray":{"type":"array","items":{"oneOf":[{"type":"string"},{"type":"number"}]}},"withDescription":{"type":"string"},"inferredUnknown":{},"uuid":{"type":"string"},"string":{"type":"string"},"maybeString":{"type":"string"},"text":{"type":"string"},"integer":{"type":"number"},"float":{"type":"number"},"decimal":{"type":"string"},"datetime":{"type":"string"},"timestamp":{"type":"string"},"time":{"type":"string"},"date":{"type":"string"},"boolean":{"type":"boolean"},"arrayString":{"type":"array","items":{"type":"string"}},"maybeArrayString":{"oneOf":[{"type":"array","items":{"type":"string"}},{"type":"null"}]},"json":{"type":"object","additionalProperties":"true"},"jsonb":{"type":"object","additionalProperties":"true"},"daterange":{},"enum":{},"virtualUpcasedString":{"oneOf":[{"type":"string"},{"type":"null"}]},"loljk":{"enum":["never"]},"delegatedMaybeString":{"type":"string"},"modelOverridden":{},"resourceOverridden":{},"withComment":{"oneOf":[{"type":"string"},{"type":"null"}]},"withParsedComment":{"oneOf":[{"type":"string"},{"type":"null"}]},"defaultedBoolean":{"type":"boolean"},"defaultedAt":{"type":"string"}},"required":["id","type","assertedString","assertedNumber","assertedBoolean","assertedNull","assertedUnknown","assertedObject","assertedMaybeObject","assertedArrayRecord","assertedUnion","assertedUnionArray","withDescription","inferredUnknown","uuid","string","maybeString","text","integer","float","decimal","datetime","timestamp","time","date","boolean","arrayString","maybeArrayString","json","jsonb","daterange","enum","virtualUpcasedString","loljk","delegatedMaybeString","modelOverridden","resourceOverridden","withComment","withParsedComment","defaultedBoolean","defaultedAt"],"additionalProperties":false}}}
2 changes: 2 additions & 0 deletions spec/example/test/files/multifile/Exhaustive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Model = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
/** This is a parsed JSON comment. */
withParsedComment: Maybe<string>;
defaultedBoolean: boolean;
defaultedAt: string;
relationships: {};
Expand Down
2 changes: 2 additions & 0 deletions spec/example/test/files/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
/** This is a parsed JSON comment. */
withParsedComment: Maybe<string>;
defaultedBoolean: boolean;
defaultedAt: string;
relationships: {};
Expand Down
2 changes: 2 additions & 0 deletions spec/example/test/files/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export type Exhaustive = {
resourceOverridden: unknown;
/** This is a comment. */
withComment: Maybe<string>;
/** This is a parsed JSON comment. */
withParsedComment: Maybe<string>;
defaultedBoolean: boolean;
defaultedAt: string;
relationships: {};
Expand Down