Merge pull request #6250 from tchak/fix-validation-json

Handle GeoJSON validation errors
This commit is contained in:
Paul Chavard 2021-06-02 20:07:50 +02:00 committed by GitHub
commit 8c889797ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 23 deletions

View file

@ -15,18 +15,26 @@ class Champs::CarteController < ApplicationController
if geo_area.nil?
geo_area = champ.geo_areas.build(source: params_source, properties: {})
save_feature!(geo_area, create_params_feature)
end
if save_feature(geo_area, create_params_feature)
render json: { feature: geo_area.to_feature }, status: :created
else
render json: { errors: geo_area.errors.full_messages }, status: :unprocessable_entity
end
else
render json: { feature: geo_area.to_feature }, status: :ok
end
end
def update
champ = policy_scope(Champ).find(params[:champ_id])
geo_area = champ.geo_areas.find(params[:id])
save_feature!(geo_area, update_params_feature)
if save_feature(geo_area, update_params_feature)
head :no_content
else
render json: { errors: geo_area.errors.full_messages }, status: :unprocessable_entity
end
end
def destroy
@ -66,13 +74,13 @@ class Champs::CarteController < ApplicationController
end
end
def save_feature!(geo_area, feature)
def save_feature(geo_area, feature)
if feature[:geometry]
geo_area.geometry = feature[:geometry]
end
if feature[:properties]
geo_area.properties.merge!(feature[:properties])
end
geo_area.save!
geo_area.save
end
end

View file

@ -41,15 +41,25 @@ describe Champs::CarteController, type: :controller do
post :create, params: params
end
context 'success' do
it { expect(response.status).to eq 201 }
end
describe 'PATCH #update' do
before do
patch :update, params: params
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
let(:params) do
{
champ_id: champ.id,
feature: feature,
source: GeoArea.sources.fetch(:selection_utilisateur)
}
end
context 'update geometry' do
it { expect(response.status).to eq 422 }
end
end
describe 'PATCH #update' do
let(:params) do
{
champ_id: champ.id,
@ -58,6 +68,11 @@ describe Champs::CarteController, type: :controller do
}
end
before do
patch :update, params: params
end
context 'update geometry' do
it { expect(response.status).to eq 204 }
end
@ -69,19 +84,18 @@ describe Champs::CarteController, type: :controller do
}
}
end
let(:params) do
{
champ_id: champ.id,
id: geo_area.id,
feature: feature
}
end
it {
expect(response.status).to eq 204
expect(geo_area.reload.description).to eq('un point')
}
end
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
it { expect(response.status).to eq 422 }
end
end
describe 'DELETE #destroy' do