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 @@ -10,7 +10,8 @@ class Config
:maybe_as_union,
:array_bracket_notation,
:infer_default_as_non_null,
:ar_comment_to_string
:ar_comment_to_string,
:infer_ar_enums

def initialize
@ar_column_to_type = nil
Expand All @@ -24,6 +25,7 @@ def initialize
@array_bracket_notation = nil
@infer_default_as_non_null = nil
@ar_comment_to_string = nil
@infer_ar_enums = nil
end
end
end
5 changes: 4 additions & 1 deletion lib/anchor/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ def anchor_attributes_properties(included_fields:)
serializer_defined = (_model_class.try(:attribute_types) || {})[model_method.to_s].respond_to?(:coder)
method_defined = model_method_defined || resource_method_defined || serializer_defined

enum = Anchor.config.infer_ar_enums && !method_defined && _model_class.try(:defined_enums).try(:[], model_method.to_s)
column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
if column
if enum
Anchor::Types::Union.new(enum.map { |_key, val| Anchor::Types::Literal.new(val) })
elsif column
type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
unless description
description = column.comment if Anchor.config.use_active_record_comment
Expand Down
2 changes: 2 additions & 0 deletions spec/example/app/models/exhaustive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ class Exhaustive < ApplicationRecord
validates :maybe_string, presence: true

def model_overridden = "model_overridden"

enum :enum, { enum_sample: "sample", enum_enum: "enum", enum_value: "value" }
end
1 change: 1 addition & 0 deletions spec/example/config/initializers/anchor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Anchor
c.use_active_record_validations = true
c.infer_default_as_non_null = true
c.infer_nullable_relationships_as_optional = true
c.infer_ar_enums = true

c.ar_column_to_type = lambda { |column|
return Types::Literal.new("never") if column.name == "loljk"
Expand Down
2 changes: 1 addition & 1 deletion spec/example/test/files/all_fields_false_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export type Exhaustive = {
json: Record<string, unknown>;
jsonb: Record<string, unknown>;
daterange: unknown;
enum: unknown;
enum: "sample" | "enum" | "value";
virtualUpcasedString: Maybe<string>;
loljk: "never";
delegatedMaybeString: string;
Expand Down
2 changes: 1 addition & 1 deletion spec/example/test/files/excluded_fields_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type Exhaustive = {
json: Record<string, unknown>;
jsonb: Record<string, unknown>;
daterange: unknown;
enum: unknown;
enum: "sample" | "enum" | "value";
virtualUpcasedString: Maybe<string>;
loljk: "never";
delegatedMaybeString: string;
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"}]},"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}}}
{"$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":{"oneOf":[{"enum":["sample"]},{"enum":["enum"]},{"enum":["value"]}]},"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: 1 addition & 1 deletion spec/example/test/files/multifile/Exhaustive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Model = {
json: Record<string, unknown>;
jsonb: Record<string, unknown>;
daterange: unknown;
enum: unknown;
enum: "sample" | "enum" | "value";
virtualUpcasedString: Maybe<string>;
loljk: "never";
delegatedMaybeString: string;
Expand Down
2 changes: 1 addition & 1 deletion spec/example/test/files/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export type Exhaustive = {
json: Record<string, unknown>;
jsonb: Record<string, unknown>;
daterange: unknown;
enum: unknown;
enum: "sample" | "enum" | "value";
virtualUpcasedString: Maybe<string>;
loljk: "never";
delegatedMaybeString: string;
Expand Down
2 changes: 1 addition & 1 deletion spec/example/test/files/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type Exhaustive = {
json: Record<string, unknown>;
jsonb: Record<string, unknown>;
daterange: unknown;
enum: unknown;
enum: "sample" | "enum" | "value";
virtualUpcasedString: Maybe<string>;
loljk: "never";
delegatedMaybeString: string;
Expand Down