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

Rails and JSON API in 2019

Rails and JSON API in 2019

Akinori MUSHA

May 07, 2019
Tweet

More Decks by Akinori MUSHA

Other Decks in Technology

Transcript

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

    View full-size slide

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

    View full-size slide

  3. JSON:API
    • RESTfulͳAPIʹ͓͍ͯɺREST͕نఆ͍ͯ͠ͳ͍͕΅Μ΍ΓఆࣜԽ͖ͯͨ͠ϓϥΫςΟεΛ
    ن֨ͱͯ͠औΓ·ͱΊ͍ͯΔ΋ͷ
    https://jsonapi.org/
    • JSONΛϕʔεͱͨ͠ཁٻɾԠ౴ϑΥʔϚοτΛࡦఆ
    • HALͳͲͷൃలܥɺܾఆ൛
    • ཤྺ
    • 2018೥12݄ 1.1RC
    • 2015೥ 5݄ 1.0࠷ऴ൛
    • 2013೥ 5݄ ࠷ॳͷυϥϑτ

    View full-size slide

  4. RESTͱ͸
    • 2000೥ɺApache HTTP ServerϓϩδΣΫτͷcofounderͰHTTP/1.1ͷࡦఆʹ
    ΋ؔΘͬͨRoy Fielding͕ത࢜߸࿦จͰఏএ
    • REpresentational State Transferͷ಄ࣈޠ
    • REpresentational State = දݱՄೳͳঢ়ଶ = ͍ΘΏΔϦιʔε
    • Transfer = సૹ
    • HTTPʹ԰্԰ΛՍͨ͠ϝλRPCϓϩτίϧʢSOAP΍XMLRPCʣɺHTTPΛ୯
    ͳΔτϥϯεϙʔτɾτϯωϧͱͯ͠࢖͖ͬͯͨ༻๏ʹର͢ΔΞϯνςʔθ
    • HTTPݪཧओٛ

    View full-size slide

  5. JSONͱ͸
    • 2001೥ɺDouglas Crockford͕WebϕʔεͷΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹʮൃݟʯͨ͠σʔλަ׵ϑΥʔϚοτ
    • The JSON Saga

    • ୯ͳΔJavaScriptͷΦϒδΣΫτϦςϥϧͰ͸HTMLυΩϡϝϯτʹຒΊࠐΉࡍʹ·͍ͣͱؾ͍ͮͨ
    • ΩʔΛඞͣΫΦʔτ͢Δ
    • ES3Ͱ͸༧໿ޠΛΩʔʹͰ͖ͳ͔ͬͨ(ES5ͰՄೳʹͳͬͨ)
    • Pythonͷdictionaryͷه๏ͱ΋Ұக͍ͯͨ͠
    • eval("{a:42}")ͯ͠ΈΔͱ෼͔Δ͕ɺϒϩοΫ಺ʹϥϕϧa:ͱࣜ42͕͋Δͱղऍ͞Εͯ͠·͏ɺͱ͍͏ཧ༝΋͋Δ
    • จࣈྻϦςϥϧʹ৑௕ͳ \ Λڐ͢
    • Λ <\/script> ͱ͢ΔͳͲͯ͠ແޮԽͰ͖ΔΑ͏ʹ
    • evalͰ͖Δͱ͍͏ख͔ܰ͞Β࢝·͕ͬͨɺ҆શΛ֬ೝੑ͢Δͷ͕େม
    • ઐ༻ύʔαʔ͕ඞཁ(JSON.parse)

    View full-size slide

  6. RESTͱ͸
    • 4ͭͷݪଇ
    • Addressability
    ͢΂ͯͷૢ࡞ର৅͸URIʢϢχόʔαϧʹϢχʔΫͳ໊લʣͰࣔ͞ΕΔ
    • Statelessness
    ηογϣϯ౳ͷঢ়ଶ؅ཧ͸ͤͣɺݸʑͷૢ࡞͕ಠཱ͍ͯ͠Δ
    • Connectedness
    ৘ใͷؔ࿈͸URIͷϦϯΫͰࣔ͢
    • Uniform Interface
    HTTPϝιουΛڞ௨ޠኮͱ͠ɺ͋ΒΏΔૢ࡞ΛGET, POST, PUT, DELETE౳Ͱද͢

    View full-size slide

  7. OpenAPI Specification
    • 2017೥ʹग़ͨɺRESTful APIͷ࢓༷Λهड़͢ΔͨΊͷݴޠ
    ʢJSON|YAMLʣ
    • https://www.openapis.org/
    • Swagger Specification (ʙ2.0)ͷޙܧ
    • Swagger͸RESTful APIΛߏங͢ΔͨΊͷϑϨʔϜϫʔΫɾ
    πʔϧʢ2011೥ʙʣ

    View full-size slide

  8. 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

    View full-size slide

  9. OpenAPI Specification (3.0)
    • ΤϯυϙΠϯτͱཁٻɾԠ౴ͷσʔλεΩʔϚͳͲΛแׅతʹهड़Ͱ͖Δ
    • JSON:APIɺHALͱ͸௚ߦͨ֓͠೦
    • JSON SchemaΛऔΓࠐΜͰεΩʔϚఆٛݴޠͱͯ͠ར༻͍ͯ͠Δ
    • ׬શʹಉظɾ௥ਵ͍ͯ͠ΔΘ͚Ͱ͸ͳ͍(JSON Schema͸௕͖ʹ౉ΓυϥϑτΛॏͶ
    ͍ͯͯɺख໭Γ΋ଟ͍)
    • JSON Hyper Schemaͱ͸গ͔͠Ϳ͍ͬͯΔ
    • OpenAPIࣗ਎͕ಠࣗʹΤϯυϙΠϯτ৘ใͷهड़ݴޠΛ͍࣋ͬͯΔ
    • OpenAPI͸ΫΤϦʔύϥϝʔλ΍ύε಺ύϥϝʔλʹ͍ͭͯ΋bodyಉ༷ʹ౷Ұతͳ
    จ๏Ͱهड़Ͱ͖ΔΑ͏ʹਐԽ

    View full-size slide

  10. 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

    View full-size slide

  11. JSON Schema
    • JSON Schema Core
    • JSON Schemaͷج൫࢓༷
    εΩʔϚόʔδϣϯͷએݴɺωετɺ಺෦ɾ֎෦ࢀরͳͲʹ͍ͭͯنఆ͞Ε͍ͯΔ
    $schema, $ref, $idͳͲ
    ͦͷଞͷ۩ମతͳޠኮ͸͜͜Ͱ͸نఆ͞Ε͍ͯͳ͍
    • JSON Schema Validation
    • JSON Schemaͷ࢓༷ͷຊମ
    σʔλܕɺόϦσʔγϣϯϧʔϧΛهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε͍ͯΔ
    ೔෇ɺ࣌ࠁɺϝʔϧΞυϨεɺURIͳͲ΋σʔλܕͱͯ͠ఆٛ͞Ε͍ͯΔ(JSONදݱࣗମ͸String)

    View full-size slide

  12. JSON Schema
    • JSON Hyper-Schema
    • ϋΠύʔϝσΟΞ(ϦϯΫ)Λهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε
    ͍ͯΔ
    • Relative JSON Pointers
    • JSONυΩϡϝϯτ಺ͷಛఆͷΩʔΛࢀর͢ΔͨΊͷه๏
    $refͰ # Ҏ߱ʹهड़͢ΔΞϯΧʔͰ࢖ΘΕΔ

    View full-size slide

  13. RailsͰJSON:API

    View full-size slide

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

    View full-size slide

  15. OpenAPI 3.0Ͱهड़ͯ͠Έ͍ͨ
    • committee gemΛ࢖ͬͯΈͨ
    γϯϓϧʹཁٻɾԠ౴όϦσʔγϣϯ͚ͩΛ௥Ճ͢ΔRackϛ
    υϧ΢ΣΞू
    OpenAPI 3.0ରԠ͕ૣ͘ɺͦͪΒʹد͍ͤͯ͘ಈ͖΋޷·͍͠
    • swagger-blocksͷΑ͏ͳRubyͷDSL͸஍ཕ…
    OpenAPI 3.0ରԠ͕ݟ͑ͳ͍

    View full-size slide

  16. JSON:APIͰ΍ͬͯΈ͍ͨ
    • JSONγϦΞϥΠβʔͷબ୒͕େࣄ
    • jbuilder
    • ActiveModel::Serializers
    • fast_jsonapi
    • rabl

    View full-size slide

  17. JSONγϦΞϥΠβʔͷൺֱ
    • jbuilder
    • DSL͕͍·͍ͪ
    • ଎͘΋ͳ͍
    • ActiveModel::Serializers
    • ྑ͘΋ѱ͘΋σϑΝΫτελϯμʔυ
    • ϓϩδΣΫτ͕ಈ͍͍ͯͳ͍
    • 0.10.xͰ೩͑ਚ͖ͨʁ

    View full-size slide

  18. JSONγϦΞϥΠβʔͷൺֱ
    • fast_jsonapi
    • Netflix੡ɺ଎ͯ͘Αͦ͞͏
    • JSON:APIʹدͤͨσϑΥϧτ
    • AMSʹ͍ۙ࢖͍৺஍
    • ొ৔͸৽͍͕͠ɺ͞΄Ͳ։ൃ͕׆ൃͱ͍͏Θ͚Ͱ͸ͳ͍
    • rabl
    • Ϟσϧʹର͢ΔserializerɺͰ͸ͳ͘ϏϡʔςϯϓϨʔτɺίϯϙʔωϯτܕ
    • චऀ͕࢖͍׳Ε͍ͯΔ

    View full-size slide

  19. Rabl
    • RubyʹΑΔDSLͱ͍͏ͷ͸ଞͱಉ͡
    • RailsͷϏϡʔͱͯ͠ಈ͘
    • JSONΛੜ੒͢ΔςϯϓϨʔτ (*.json.rabl)
    • ଞͷγϦΞϥΠβʔ͸جຊతʹϞσϧͱJSONදݱ͕1:1͕ͩɺRabl͸ίϯςΩ
    ετʹԠͯࣗ͡༝ʹςϯϓϨʔτΛબͼɺదٓpartialͰऔΓࠐΊΔ
    • OpenAPIͷcomponentsͰϞσϧΛఆٛͯ͠$refͰࢀরɺͱ͍͏Ϟσϧͱ
    ૬ੑ΋ྑ͍͸ͣ
    • rabl-railsΛ࢖͏ͱ݁ߏ଎͍

    View full-size slide

  20. rabl-rails
    • rablຊՈ΋RailsʹରԠ͍ͯ͠Δ͕ɺςϯϓϨʔτΤϯδϯͱ
    ͯ͠rablΛ࢖͍ͭͭɺίϯύΠϧͱల։ͷεςʔδΛ෼͚ͯ
    ੑೳΛେ෯ʹ্͛Δrabl-railsͱ͍͏gem͕͋Δ
    • ͜Ε͸ͦͷ࢓૊Έ্ɺΠϯελϯεม਺Λͦͷ··࢖͑ͳ͍
    ͳͲॻ͖ํ͕গ͠มΘΔ
    • rablຊՈͷWikiͳͲͷྫΛಡΈସ͑ɾॻ͖׵͑Δඞཁ͕ग़
    ͯ͘Δ

    View full-size slide

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

    View full-size slide

  22. ࣮ྫ
    • 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)Λ࢖͏Α͏ʹͨ͠

    View full-size slide

  23. ײ૝
    OpenAPI͸ɺSwagger 2.0͔ΒҠߦͰ͖ͳ͍πʔϧ͕ଟͦ͏ͩ
    ͕ɺҰ͔Β࢖͏෼ʹ͸࢖͍উखྑ͍
    JSON:APIʹ͍ͭͯ͸ɺ४ڌ͕Ͳͷ͘Β͍͏Ε͍͔͠͸Θ͔Βͳ͍
    Ұํɺ࣮૷͸JSON:APIରԠΛ͏ͨ͏γϦΞϥΠβ͔ɺrablͷΑ͏
    ʹॊೈੑͷߴ͍΋ͷΛ࢖Θͳ͍ͱ໘౗
    JSON SchemaͰஸೡʹॻ͍࣮ͯ૷ͱ྆ํϝϯςφϯε͢Δͷ͸
    ͭΒ͍…

    View full-size slide