Skip to content

Commit

Permalink
Handle json-breaking double quotes in pbcore values in JSON API (#2696)
Browse files Browse the repository at this point in the history
* Fixes 500 errors by re-escape double quotes in xml before json transformation
* Sends 404 when Solr record not found via json api.
* Caches pbcore xsl for json XSLT doc
  • Loading branch information
foglabs authored Dec 11, 2023
1 parent 955cb4a commit 502cb1e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ def index
def show
@solr = Solr.instance.connect
data = @solr.get('select', params: { q: "id:#{params[:id]}", fl: 'xml' })

return render_not_found(params[:id]) unless data['response']['docs'] && data['response']['docs'][0]
xml = data['response']['docs'][0]['xml']

respond_to do |format|
format.xml do
render text: xml
end

format.json do
pbcore_xml_to_json_xsl_doc = Nokogiri::XSLT(File.read('./lib/pbcore_xml_to_json.xsl'))
# escape double quotes (because they may appear in node values)
xml = xml.gsub(%(\"), %(\\\"))

json = pbcore_xml_to_json_xsl_doc.transform(Nokogiri::XML(xml))
render json: JSON.pretty_generate(
JSON.parse(
pbcore_xml_to_json_xsl_doc.transform(
Nokogiri::XML(xml)
)
)
JSON.parse(json)
)
end
end
Expand All @@ -74,4 +76,16 @@ def transcript
def render_no_transcript_content
render json: { status: '404 Not Found', code: '404', message: 'This transcript is not currently available' }, status: :not_found
end

def render_not_found(guid)
render json: { status: '404 Not Found', code: '404', message: "Record #{guid} not found" }, status: :not_found
end

private

def pbcore_xml_to_json_xsl_doc
Rails.cache.fetch("pbcore_xml_to_json_xsl_doc") do
return Nokogiri::XSLT(File.read('./lib/pbcore_xml_to_json.xsl'))
end
end
end

0 comments on commit 502cb1e

Please sign in to comment.