Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails and JSON API in 2019

Rails and JSON API in 2019

74f896b312b786ee75a18073941e2457?s=128

Akinori MUSHA

May 07, 2019
Tweet

Transcript

  1. Rails and JSON API In 2019 Akinori Musha (@knu)

  2. JSON API In 2019 • JSON:API • OpenAPI

  3. JSON:API

  4. JSON:API • RESTfulͳAPIʹ͓͍ͯɺREST͕نఆ͍ͯ͠ͳ͍͕΅Μ΍ΓఆࣜԽ͖ͯͨ͠ϓϥΫςΟεΛ ن֨ͱͯ͠औΓ·ͱΊ͍ͯΔ΋ͷ https://jsonapi.org/ • JSONΛϕʔεͱͨ͠ཁٻɾԠ౴ϑΥʔϚοτΛࡦఆ • HALͳͲͷൃలܥɺܾఆ൛ •

    ཤྺ • 2018೥12݄ 1.1RC • 2015೥ 5݄ 1.0࠷ऴ൛ • 2013೥ 5݄ ࠷ॳͷυϥϑτ
  5. RESTͱ͸ • 2000೥ɺApache HTTP ServerϓϩδΣΫτͷcofounderͰHTTP/1.1ͷࡦఆʹ ΋ؔΘͬͨRoy Fielding͕ത࢜߸࿦จͰఏএ • REpresentational State

    Transferͷ಄ࣈޠ • REpresentational State = දݱՄೳͳঢ়ଶ = ͍ΘΏΔϦιʔε • Transfer = సૹ • HTTPʹ԰্԰ΛՍͨ͠ϝλRPCϓϩτίϧʢSOAP΍XMLRPCʣɺHTTPΛ୯ ͳΔτϥϯεϙʔτɾτϯωϧͱͯ͠࢖͖ͬͯͨ༻๏ʹର͢ΔΞϯνςʔθ • HTTPݪཧओٛ
  6. JSONͱ͸ • 2001೥ɺDouglas Crockford͕WebϕʔεͷΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹʮൃݟʯͨ͠σʔλަ׵ϑΥʔϚοτ • The JSON Saga ▶ •

    ୯ͳΔJavaScriptͷΦϒδΣΫτϦςϥϧͰ͸HTMLυΩϡϝϯτʹຒΊࠐΉࡍʹ·͍ͣͱؾ͍ͮͨ • ΩʔΛඞͣΫΦʔτ͢Δ • ES3Ͱ͸༧໿ޠΛΩʔʹͰ͖ͳ͔ͬͨ(ES5ͰՄೳʹͳͬͨ) • Pythonͷdictionaryͷه๏ͱ΋Ұக͍ͯͨ͠ • eval("{a:42}")ͯ͠ΈΔͱ෼͔Δ͕ɺϒϩοΫ಺ʹϥϕϧa:ͱࣜ42͕͋Δͱղऍ͞Εͯ͠·͏ɺͱ͍͏ཧ༝΋͋Δ • จࣈྻϦςϥϧʹ৑௕ͳ \ Λڐ͢ • </script> Λ <\/script> ͱ͢ΔͳͲͯ͠ແޮԽͰ͖ΔΑ͏ʹ • evalͰ͖Δͱ͍͏ख͔ܰ͞Β࢝·͕ͬͨɺ҆શΛ֬ೝੑ͢Δͷ͕େม • ઐ༻ύʔαʔ͕ඞཁ(JSON.parse)
  7. RESTͱ͸ • 4ͭͷݪଇ • Addressability ͢΂ͯͷૢ࡞ର৅͸URIʢϢχόʔαϧʹϢχʔΫͳ໊લʣͰࣔ͞ΕΔ • Statelessness ηογϣϯ౳ͷঢ়ଶ؅ཧ͸ͤͣɺݸʑͷૢ࡞͕ಠཱ͍ͯ͠Δ •

    Connectedness ৘ใͷؔ࿈͸URIͷϦϯΫͰࣔ͢ • Uniform Interface HTTPϝιουΛڞ௨ޠኮͱ͠ɺ͋ΒΏΔૢ࡞ΛGET, POST, PUT, DELETE౳Ͱද͢
  8. OpenAPI

  9. OpenAPI Specification • 2017೥ʹग़ͨɺRESTful APIͷ࢓༷Λهड़͢ΔͨΊͷݴޠ ʢJSON|YAMLʣ • https://www.openapis.org/ • Swagger

    Specification (ʙ2.0)ͷޙܧ • Swagger͸RESTful APIΛߏங͢ΔͨΊͷϑϨʔϜϫʔΫɾ πʔϧʢ2011೥ʙʣ
  10. OpenAPI Specification (3.0) • ཤྺ • 2018-10-08 OpenAPI Specification 3.0.2

    • 2017-12-06 OpenAPI Specification 3.0.1 • 2017-07-26 OpenAPI Specification 3.0.0 • 2016-01-01 OpenAPI Initiativeൃ଍ • 2014-09-08 Swagger Specification 2.0 • 2014-03-14 Swagger Specification 1.2 • 2012-08-22 Swagger Specification 1.1 • 2011-08-10 Swagger Specification 1.0
  11. OpenAPI Specification (3.0) • ΤϯυϙΠϯτͱཁٻɾԠ౴ͷσʔλεΩʔϚͳͲΛแׅతʹهड़Ͱ͖Δ • JSON:APIɺHALͱ͸௚ߦͨ֓͠೦ • JSON SchemaΛऔΓࠐΜͰεΩʔϚఆٛݴޠͱͯ͠ར༻͍ͯ͠Δ

    • ׬શʹಉظɾ௥ਵ͍ͯ͠ΔΘ͚Ͱ͸ͳ͍(JSON Schema͸௕͖ʹ౉ΓυϥϑτΛॏͶ ͍ͯͯɺख໭Γ΋ଟ͍) • JSON Hyper Schemaͱ͸গ͔͠Ϳ͍ͬͯΔ • OpenAPIࣗ਎͕ಠࣗʹΤϯυϙΠϯτ৘ใͷهड़ݴޠΛ͍࣋ͬͯΔ • OpenAPI͸ΫΤϦʔύϥϝʔλ΍ύε಺ύϥϝʔλʹ͍ͭͯ΋bodyಉ༷ʹ౷Ұతͳ จ๏Ͱهड़Ͱ͖ΔΑ͏ʹਐԽ
  12. JSON Schema

  13. JSON Schema • JSONσʔλͷεΩʔϚͷهड़ݴޠ https://json-schema.org/ • ͦΕࣗମ͕JSONͰهड़͞ΕΔ • ҎԼͷ࢓༷ͷ૯শ •

    JSON Schema Core • JSON Schema Validation • JSON Hyper-Schema • Relative JSON Pointers • ཤྺ • 2018-03-19 draft-07
  14. JSON Schema • JSON Schema Core • JSON Schemaͷج൫࢓༷ εΩʔϚόʔδϣϯͷએݴɺωετɺ಺෦ɾ֎෦ࢀরͳͲʹ͍ͭͯنఆ͞Ε͍ͯΔ

    $schema, $ref, $idͳͲ ͦͷଞͷ۩ମతͳޠኮ͸͜͜Ͱ͸نఆ͞Ε͍ͯͳ͍ • JSON Schema Validation • JSON Schemaͷ࢓༷ͷຊମ σʔλܕɺόϦσʔγϣϯϧʔϧΛهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε͍ͯΔ ೔෇ɺ࣌ࠁɺϝʔϧΞυϨεɺURIͳͲ΋σʔλܕͱͯ͠ఆٛ͞Ε͍ͯΔ(JSONදݱࣗମ͸String)
  15. JSON Schema • JSON Hyper-Schema • ϋΠύʔϝσΟΞ(ϦϯΫ)Λهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε ͍ͯΔ • Relative

    JSON Pointers • JSONυΩϡϝϯτ಺ͷಛఆͷΩʔΛࢀর͢ΔͨΊͷه๏ $refͰ # Ҏ߱ʹهड़͢ΔΞϯΧʔͰ࢖ΘΕΔ
  16. RailsͰJSON:API

  17. RailsͰJSON:API • OpenAPI 3.0Ͱهड़ͯ͠Έ͍ͨ • JSON:APIͰ΍ͬͯΈ͍ͨ

  18. OpenAPI 3.0Ͱهड़ͯ͠Έ͍ͨ • committee gemΛ࢖ͬͯΈͨ γϯϓϧʹཁٻɾԠ౴όϦσʔγϣϯ͚ͩΛ௥Ճ͢ΔRackϛ υϧ΢ΣΞू OpenAPI 3.0ରԠ͕ૣ͘ɺͦͪΒʹد͍ͤͯ͘ಈ͖΋޷·͍͠ •

    swagger-blocksͷΑ͏ͳRubyͷDSL͸஍ཕ… OpenAPI 3.0ରԠ͕ݟ͑ͳ͍
  19. JSON:APIͰ΍ͬͯΈ͍ͨ • JSONγϦΞϥΠβʔͷબ୒͕େࣄ • jbuilder • ActiveModel::Serializers • fast_jsonapi •

    rabl
  20. JSONγϦΞϥΠβʔͷൺֱ • jbuilder • DSL͕͍·͍ͪ • ଎͘΋ͳ͍ • ActiveModel::Serializers •

    ྑ͘΋ѱ͘΋σϑΝΫτελϯμʔυ • ϓϩδΣΫτ͕ಈ͍͍ͯͳ͍ • 0.10.xͰ೩͑ਚ͖ͨʁ
  21. JSONγϦΞϥΠβʔͷൺֱ • fast_jsonapi • Netflix੡ɺ଎ͯ͘Αͦ͞͏ • JSON:APIʹدͤͨσϑΥϧτ • AMSʹ͍ۙ࢖͍৺஍ •

    ొ৔͸৽͍͕͠ɺ͞΄Ͳ։ൃ͕׆ൃͱ͍͏Θ͚Ͱ͸ͳ͍ • rabl • Ϟσϧʹର͢ΔserializerɺͰ͸ͳ͘ϏϡʔςϯϓϨʔτɺίϯϙʔωϯτܕ • චऀ͕࢖͍׳Ε͍ͯΔ
  22. Rabl • RubyʹΑΔDSLͱ͍͏ͷ͸ଞͱಉ͡ • RailsͷϏϡʔͱͯ͠ಈ͘ • JSONΛੜ੒͢ΔςϯϓϨʔτ (*.json.rabl) • ଞͷγϦΞϥΠβʔ͸جຊతʹϞσϧͱJSONදݱ͕1:1͕ͩɺRabl͸ίϯςΩ

    ετʹԠͯࣗ͡༝ʹςϯϓϨʔτΛબͼɺదٓpartialͰऔΓࠐΊΔ • OpenAPIͷcomponentsͰϞσϧΛఆٛͯ͠$refͰࢀরɺͱ͍͏Ϟσϧͱ ૬ੑ΋ྑ͍͸ͣ • rabl-railsΛ࢖͏ͱ݁ߏ଎͍
  23. rabl-rails • rablຊՈ΋RailsʹରԠ͍ͯ͠Δ͕ɺςϯϓϨʔτΤϯδϯͱ ͯ͠rablΛ࢖͍ͭͭɺίϯύΠϧͱల։ͷεςʔδΛ෼͚ͯ ੑೳΛେ෯ʹ্͛Δrabl-railsͱ͍͏gem͕͋Δ • ͜Ε͸ͦͷ࢓૊Έ্ɺΠϯελϯεม਺Λͦͷ··࢖͑ͳ͍ ͳͲॻ͖ํ͕গ͠มΘΔ • rablຊՈͷWikiͳͲͷྫΛಡΈସ͑ɾॻ͖׵͑Δඞཁ͕ग़

    ͯ͘Δ
  24. rablͱlayout • rabl͸RailsͷϏϡʔͱͯ͠ಈ͘ɺͱ͍͏͜ͱ͸JSON:APIʹԊͬͨΨϫΛ layoutͱͯ͠࡞Ε͹ɺฦ͍ͨ͠dataΛهड़͢Δ͚ͩͰࣗಈతʹԠ౴͕࡞Εͯ ศར…ʂ • ͱࢥ͍͖΍ɺrablͷDSL࣮૷ͱRubyͷߏจ্ͷ੍໿͔ΒςϯϓϨʔτ಺ʹ yieldΛஔ͘ͱจ๏Τϥʔʹͳͬͯ͠·͏ • ͭ·ΓrablͰlayoutΛॻ͘͜ͱ͸Ͱ͖ͳ͍…

    • erb΍hamlΛ࢖͑͹Ͱ͖Δ͕ॻ͖৺஍͸ྑ͘ͳ͍ • layout͸͍͍ͤͥยखͰ଍ΓΔఔ౓ͳͷͰ໨ΛͭͿΖ͏…
  25. ࣮ྫ

  26. ࣮ྫ • committeeͱrablͰ࡞ͬͯΈ·ͨ͠ • https://github.com/knu/jsonapi-example • Ԡ౴όϦσʔγϣϯ • ։ൃ؀ڥͷΈͰ༗ޮԽ •

    ςετ؀ڥͰ͸request specͰ໌ࣔతʹassertionΛೖΕΔ ༗ޮԽͯ͠͠·͏ͱHTTPεςʔλεͷ࣌఺Ͱ͚ͯ͜͠·ͬͯ௥͍ͮΒ͍ • ϓϩμΫγϣϯ؀ڥͰ͸ςετΛ௨͍ͬͯΔ͸ͣͳͷͰ৑௕ɺ࿙Εͷ෦෼ͰϋʔυΤϥʔʹͳͬͯ΋ࠔΔ ݕ஌͚ͩͰ͖Δػೳ ͕४උதͳͷͰɺ࣮૷͞ΕͨΒ࢖͏ͱΑͦ͞͏ • JSONϏϡʔ͸ΨϫlayoutɺΤϯυϙΠϯτͷςϯϓϨʔτɺcomponentʹରԠͨ͠partialɺͱOpenAPIఆٛ ʹԊͬͯ࡞੒ • ࡉ͔͍ͱ͜ΖͰ͸ɺJSON:APIͷMIME type (application/vnd.api+json)Λ࢖͏Α͏ʹͨ͠
  27. ײ૝ OpenAPI͸ɺSwagger 2.0͔ΒҠߦͰ͖ͳ͍πʔϧ͕ଟͦ͏ͩ ͕ɺҰ͔Β࢖͏෼ʹ͸࢖͍উखྑ͍ JSON:APIʹ͍ͭͯ͸ɺ४ڌ͕Ͳͷ͘Β͍͏Ε͍͔͠͸Θ͔Βͳ͍ Ұํɺ࣮૷͸JSON:APIରԠΛ͏ͨ͏γϦΞϥΠβ͔ɺrablͷΑ͏ ʹॊೈੑͷߴ͍΋ͷΛ࢖Θͳ͍ͱ໘౗ JSON SchemaͰஸೡʹॻ͍࣮ͯ૷ͱ྆ํϝϯςφϯε͢Δͷ͸ ͭΒ͍…