Merge pull request #2471 from SamantazFox/extractor-patch-2

Fix infinite recursion caused by `to_json` methods in different data structures
This commit is contained in:
Samantaz Fox 2021-11-07 21:32:02 +01:00 committed by GitHub
commit 6cabc96f75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 78 deletions

View File

@ -206,7 +206,7 @@ def create_notification_stream(env, topics, connection_channel)
video = get_video(video_id, PG_DB) video = get_video(video_id, PG_DB)
video.published = published video.published = published
response = JSON.parse(video.to_json(locale)) response = JSON.parse(video.to_json(locale, nil))
if fields_text = env.params.query["fields"]? if fields_text = env.params.query["fields"]?
begin begin
@ -282,7 +282,7 @@ def create_notification_stream(env, topics, connection_channel)
video = get_video(video_id, PG_DB) video = get_video(video_id, PG_DB)
video.published = Time.unix(published) video.published = Time.unix(published)
response = JSON.parse(video.to_json(locale)) response = JSON.parse(video.to_json(locale, nil))
if fields_text = env.params.query["fields"]? if fields_text = env.params.query["fields"]?
begin begin

View File

@ -58,17 +58,13 @@ struct SearchVideo
end end
end end
def to_xml(auto_generated, query_params, xml : XML::Builder | Nil = nil) def to_xml(auto_generated, query_params, _xml : Nil)
if xml
to_xml(HOST_URL, auto_generated, query_params, xml)
else
XML.build do |xml| XML.build do |xml|
to_xml(HOST_URL, auto_generated, query_params, xml) to_xml(auto_generated, query_params, xml)
end
end end
end end
def to_json(locale : Hash(String, JSON::Any), json : JSON::Builder) def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
json.object do json.object do
json.field "type", "video" json.field "type", "video"
json.field "title", self.title json.field "title", self.title
@ -99,14 +95,15 @@ struct SearchVideo
end end
end end
def to_json(locale, json : JSON::Builder | Nil = nil) # TODO: remove the locale and follow the crystal convention
if json def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
to_json(locale, json)
else
JSON.build do |json| JSON.build do |json|
to_json(locale, json) to_json(locale, json)
end end
end end
def to_json(json : JSON::Builder)
to_json(nil, json)
end end
def is_upcoming def is_upcoming
@ -133,7 +130,7 @@ struct SearchPlaylist
property videos : Array(SearchPlaylistVideo) property videos : Array(SearchPlaylistVideo)
property thumbnail : String? property thumbnail : String?
def to_json(locale, json : JSON::Builder) def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
json.object do json.object do
json.field "type", "playlist" json.field "type", "playlist"
json.field "title", self.title json.field "title", self.title
@ -163,14 +160,15 @@ struct SearchPlaylist
end end
end end
def to_json(locale, json : JSON::Builder | Nil = nil) # TODO: remove the locale and follow the crystal convention
if json def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
to_json(locale, json)
else
JSON.build do |json| JSON.build do |json|
to_json(locale, json) to_json(locale, json)
end end
end end
def to_json(json : JSON::Builder)
to_json(nil, json)
end end
end end
@ -185,7 +183,7 @@ struct SearchChannel
property description_html : String property description_html : String
property auto_generated : Bool property auto_generated : Bool
def to_json(locale, json : JSON::Builder) def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
json.object do json.object do
json.field "type", "channel" json.field "type", "channel"
json.field "author", self.author json.field "author", self.author
@ -215,14 +213,15 @@ struct SearchChannel
end end
end end
def to_json(locale, json : JSON::Builder | Nil = nil) # TODO: remove the locale and follow the crystal convention
if json def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
to_json(locale, json)
else
JSON.build do |json| JSON.build do |json|
to_json(locale, json) to_json(locale, json)
end end
end end
def to_json(json : JSON::Builder)
to_json(nil, json)
end end
end end
@ -235,7 +234,7 @@ class Category
property description_html : String property description_html : String
property badges : Array(Tuple(String, String))? property badges : Array(Tuple(String, String))?
def to_json(locale, json : JSON::Builder) def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
json.object do json.object do
json.field "type", "category" json.field "type", "category"
json.field "title", self.title json.field "title", self.title
@ -249,14 +248,15 @@ class Category
end end
end end
def to_json(locale, json : JSON::Builder | Nil = nil) # TODO: remove the locale and follow the crystal convention
if json def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
to_json(locale, json)
else
JSON.build do |json| JSON.build do |json|
to_json(locale, json) to_json(locale, json)
end end
end end
def to_json(json : JSON::Builder)
to_json(nil, json)
end end
end end

View File

@ -11,7 +11,7 @@ struct PlaylistVideo
property index : Int64 property index : Int64
property live_now : Bool property live_now : Bool
def to_xml(auto_generated, xml : XML::Builder) def to_xml(xml : XML::Builder)
xml.element("entry") do xml.element("entry") do
xml.element("id") { xml.text "yt:video:#{self.id}" } xml.element("id") { xml.text "yt:video:#{self.id}" }
xml.element("yt:videoId") { xml.text self.id } xml.element("yt:videoId") { xml.text self.id }
@ -20,13 +20,8 @@ struct PlaylistVideo
xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}") xml.element("link", rel: "alternate", href: "#{HOST_URL}/watch?v=#{self.id}")
xml.element("author") do xml.element("author") do
if auto_generated
xml.element("name") { xml.text self.author } xml.element("name") { xml.text self.author }
xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" } xml.element("uri") { xml.text "#{HOST_URL}/channel/#{self.ucid}" }
else
xml.element("name") { xml.text author }
xml.element("uri") { xml.text "#{HOST_URL}/channel/#{ucid}" }
end
end end
xml.element("content", type: "xhtml") do xml.element("content", type: "xhtml") do
@ -47,17 +42,11 @@ struct PlaylistVideo
end end
end end
def to_xml(auto_generated, xml : XML::Builder? = nil) def to_xml(_xml : Nil = nil)
if xml XML.build { |xml| to_xml(xml) }
to_xml(auto_generated, xml)
else
XML.build do |xml|
to_xml(auto_generated, xml)
end
end
end end
def to_json(locale, json : JSON::Builder, index : Int32?) def to_json(json : JSON::Builder, index : Int32? = nil)
json.object do json.object do
json.field "title", self.title json.field "title", self.title
json.field "videoId", self.id json.field "videoId", self.id
@ -81,14 +70,8 @@ struct PlaylistVideo
end end
end end
def to_json(locale, json : JSON::Builder? = nil, index : Int32? = nil) def to_json(_json : Nil, index : Int32? = nil)
if json JSON.build { |json| to_json(json, index: index) }
to_json(locale, json, index: index)
else
JSON.build do |json|
to_json(locale, json, index: index)
end
end
end end
end end
@ -144,7 +127,7 @@ struct Playlist
json.array do json.array do
videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id) videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id)
videos.each do |video| videos.each do |video|
video.to_json(locale, json) video.to_json(json)
end end
end end
end end
@ -224,7 +207,7 @@ struct InvidiousPlaylist
videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id) videos = get_playlist_videos(PG_DB, self, offset: offset, locale: locale, video_id: video_id)
videos.each_with_index do |video, index| videos.each_with_index do |video, index|
video.to_json(locale, json, offset + index) video.to_json(json, offset + index)
end end
end end
end end

View File

@ -274,7 +274,10 @@ module Invidious::Routes::API::V1::Authenticated
env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}" env.response.headers["Location"] = "#{HOST_URL}/api/v1/auth/playlists/#{plid}/videos/#{playlist_video.index.to_u64.to_s(16).upcase}"
env.response.status_code = 201 env.response.status_code = 201
playlist_video.to_json(locale, index: playlist.index.size)
JSON.build do |json|
playlist_video.to_json(json, index: playlist.index.size)
end
end end
def self.delete_video_in_playlist(env) def self.delete_video_in_playlist(env)

View File

@ -16,7 +16,7 @@ module Invidious::Routes::API::V1::Videos
return error_json(500, ex) return error_json(500, ex)
end end
video.to_json(locale) video.to_json(locale, nil)
end end
def self.captions(env) def self.captions(env)

View File

@ -281,9 +281,7 @@ module Invidious::Routes::Feeds
xml.element("name") { xml.text playlist.author } xml.element("name") { xml.text playlist.author }
end end
videos.each do |video| videos.each &.to_xml(xml)
video.to_xml(false, xml)
end
end end
end end
else else

View File

@ -275,7 +275,7 @@ struct Video
end end
end end
def to_json(locale : Hash(String, JSON::Any), json : JSON::Builder) def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)
json.object do json.object do
json.field "type", "video" json.field "type", "video"
@ -474,14 +474,13 @@ struct Video
end end
end end
def to_json(locale, json : JSON::Builder | Nil = nil) # TODO: remove the locale and follow the crystal convention
if json def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)
to_json(locale, json) JSON.build { |json| to_json(locale, json) }
else
JSON.build do |json|
to_json(locale, json)
end
end end
def to_json(json : JSON::Builder | Nil = nil)
to_json(nil, json)
end end
def title def title