Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle json mapping exception #7006

Merged
merged 1 commit into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,9 +23,10 @@ import {
forManagementAsAppUser,
forManagementAsSimpleUser,
} from '@gravitee/utils/configuration';
import { created, noContent, unauthorized } from '@lib/jest-utils';
import { created, fail, noContent, unauthorized } from '@lib/jest-utils';
import { ApplicationEntity } from '@gravitee/management-webclient-sdk/src/lib/models/ApplicationEntity';
import { ApplicationsFaker } from '@gravitee/fixtures/management/ApplicationsFaker';
import { NewApiEntityFlowModeEnum, NewApplicationEntityOriginEnum } from '../../../lib/management-webclient-sdk/src/lib/models';

const managementApplicationsApiAsAdminUser = new ApplicationsApi(forManagementAsAdminUser());
const managementApplicationsApiAsAppUser = new ApplicationsApi(forManagementAsAppUser());
Expand Down Expand Up @@ -67,6 +68,18 @@ describe('API Management Application tests', function () {
}),
);
});

test('should fail because of origin is invalid', async () => {
await fail(
applicationResource.createApplicationRaw({
orgId,
envId,
newApplicationEntity: ApplicationsFaker.newApplication({ origin: 'dummy' as NewApplicationEntityOriginEnum }),
}),
400,
'Cannot deserialize value of type `io.gravitee.definition.model.Origin` from String "dummy": not one of the values accepted for Enum class: [MANAGEMENT, KUBERNETES]',
);
});
});
});

Expand Down
Expand Up @@ -23,10 +23,11 @@ import {
forPortalAsAppUser,
forPortalAsSimpleUser,
} from '@gravitee/utils/configuration';
import { created, noContent, unauthorized } from '@lib/jest-utils';
import { created, fail, noContent, unauthorized } from '@lib/jest-utils';
import { ApplicationApi } from '@gravitee/portal-webclient-sdk/src/lib/apis/ApplicationApi';
import { Application } from '@gravitee/portal-webclient-sdk/src/lib/models/Application';
import { PortalApplicationFaker } from '@gravitee/fixtures/portal/PortalApplicationFaker';
import { ApiKeyModeEnum } from '../../../lib/portal-webclient-sdk/src/lib';

const portalApplicationApiAsAdminUser = new ApplicationApi(forPortalAsAdminUser());
const portalApplicationApiAsAppUser = new ApplicationApi(forPortalAsAppUser());
Expand Down Expand Up @@ -61,6 +62,16 @@ describe('Portal application tests', () => {
);
});
});

test('should fail because of api key mode is invalid', async () => {
await fail(
portalApplicationApiAsAdminUser.createApplicationRaw({
applicationInput: PortalApplicationFaker.newApplicationInput({ api_key_mode: 'dummy' as ApiKeyModeEnum }),
}),
400,
"Cannot construct instance of `io.gravitee.rest.api.portal.rest.model.ApiKeyModeEnum`, problem: Unexpected value 'dummy'",
);
});
});

describe('Portal application tests with unauthorized users', () => {
Expand Down
Expand Up @@ -18,7 +18,7 @@ import faker from '@faker-js/faker';
import { NewApplicationEntity } from '@gravitee/management-webclient-sdk/src/lib/models/NewApplicationEntity';

export class ApplicationsFaker {
static newApplication(attributes?: Partial<NewApiEntity>): NewApplicationEntity {
static newApplication(attributes?: Partial<NewApplicationEntity>): NewApplicationEntity {
const name = faker.commerce.productName();
const description = faker.commerce.productDescription();

Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.rest.api.management.rest.provider;

import com.fasterxml.jackson.databind.JsonMappingException;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.rest.api.management.rest.model.ErrorEntity;
import javax.annotation.Priority;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

@Provider
@Priority(1)
public class JsonMappingExceptionMapper extends AbstractExceptionMapper<JsonMappingException> {

@Override
public Response toResponse(JsonMappingException exception) {
return Response
.status(Response.Status.BAD_REQUEST)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(new ErrorEntity(exception.getOriginalMessage(), HttpStatusCode.BAD_REQUEST_400))
.build();
}
}
Expand Up @@ -29,8 +29,6 @@
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.Property;
import io.swagger.util.Json;
import io.swagger.v3.jaxrs2.SwaggerSerializers;
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Date;
Expand Down Expand Up @@ -95,6 +93,7 @@ public Model resolve(Type type, ModelConverterContext context, Iterator<ModelCon
register(NotAllowedExceptionMapper.class);
register(BadRequestExceptionMapper.class);
register(EnumParamConverterProvider.class);
register(JsonMappingExceptionMapper.class);

register(SecurityContextFilter.class);
register(PermissionsFilter.class);
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.rest.api.portal.rest.provider;

import com.fasterxml.jackson.databind.JsonMappingException;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.rest.api.portal.rest.model.Error;
import javax.annotation.Priority;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

@Provider
@Priority(1)
public class JsonMappingExceptionMapper extends AbstractExceptionMapper<JsonMappingException> {

@Override
public Response toResponse(JsonMappingException exception) {
return Response
.status(Response.Status.BAD_REQUEST)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(new Error().status(Integer.toString(HttpStatusCode.BAD_REQUEST_400)).message(exception.getOriginalMessage()))
.build();
}
}
Expand Up @@ -55,6 +55,7 @@ public GraviteePortalApplication(AuthenticationProviderManager authenticationPro
register(NotAllowedExceptionMapper.class);
register(BadRequestExceptionMapper.class);
register(QueryParamExceptionMapper.class);
register(JsonMappingExceptionMapper.class);

register(SecurityContextFilter.class);
register(GraviteeContextRequestFilter.class);
Expand Down