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

EC新サービスにおけるスキーマファースト開発 / Schema First Developme...

EC新サービスにおけるスキーマファースト開発 / Schema First Development in the New EC Service

EC Tech MTG #4(GMOペパボEC事業部の社内勉強会)のための資料です

Kōhei Yamamoto

October 24, 2017
Tweet

More Decks by Kōhei Yamamoto

Other Decks in Technology

Transcript

  1. OpenAPIυΩϡϝϯτͷྫʢൈਮʣ definitions: Shipment: type: object properties: id: type: integer example:

    1 shipped_at: type: - string - "null" format: date-time example: 2017-07-10T12:00:00Z paths: /admin/shipments/{shipmentId}: get: parameters: - shipmentId: in: path name: shipmentId type: integer required: true responses: '200': schema: type: object properties: shipment: $ref: '#/definitions/Shipment' ˡΤϯυϙΠϯτͷఆٛ ˣϨεϙϯεߏ଄ͷఆٛ
  2. Swagger tools • Swagger UI / Swagger Editor wOpenAPIυΩϡϝϯτ͔Βϒϥ΢βͰݟ΍͍͢υΩϡϝϯτΛੜ੒ •

    Swagger Codegen wOpenAPIυΩϡϝϯτ͔Β֤छݴޠͰॻ͔ΕͨΫϥΠΞϯτͱ
 ελϒαʔόͷίʔυΛੜ੒ wࠓճ͸ελϒαʔόΛੜ੒࣮ͯ͠API։ൃͱฒߦͨ͠ΫϥΠΞϯτ
 ։ൃΛ࣮ࢪ
  3. JSON Schemahttp://json-schema.org w ͋ΔJSONͷߏ଄Λنఆ͢Δʢͭ·ΓεΩʔϚʣJSON type: object properties: id: type: string

    email: type: string role: type: array items: type: string enum: - admin - normal // όϦσʔγϣϯOK { "id": "1", "email": "[email protected]", "role": "normal" } // όϦσʔγϣϯNGʢid͕integerʣ { "id": 1 "email": "[email protected]" } id, email, roleΛଐੑʹ࣋ͭJSON Schema JSONʹର͢ΔόϦσʔγϣϯ͕Մೳ
  4. request spec࣮ߦ࣌ͷόϦσʔγϣϯ describe 'GET /shipments/:id' do it 'returns 200 OK'

    do get "/shipments/#{shipment.id}" end end HTTPϦΫΤετΛൃߦͨ͋͠ͱʹϨεϙϯεͷJSONΛCommitteeͰ
 νΣοΫ assert_schema_conform ͢ΔΑ͏ʹॲཧࠩ͠ସ͑ ։ൃ࣌ʹεΩʔϚʹΑΔνΣοΫΛҙࣝ͠ͳ͍ͰΑ͘ͳͬͨʂ # ActionDispatch::Integration::Sessionʹprepend def process(*args) super *args assert_schema_conform end ˞งғؾͳͷͰৄࡉ͸ϒϩάݟͯͶ http://blog.kymmt.com/entry/auto-json-validation-with-committee
  5. όϦσʔγϣϯࣦഊ࣌ͷྫ 1) Users GET /users/:id conforms to the schema Failure/Error:

    assert_schema_conform Committee::InvalidResponse: Invalid response. #: failed schema #/properties//users/{userId}/properties/GET: "email" wasn't supplied. request specΛॻ͍͓͚ͯ͹ɺOpenAPIυΩϡϝϯτͷϨεϙϯεεΩʔϚͱ
 ࣮ࡍͷϨεϙϯεJSON͕৯͍ҧ͏ͱ͖ʹࢦఠͯ͘͠ΕΔΑ͏ʹͳͬͨ