Skip to content

Commit

Permalink
MINOR: Fixes version lookup exception.
Browse files Browse the repository at this point in the history
Given a schema with 2 versions (0 and 1), if you pass in a version of `2` you will get an `OutOfBoundsException` instead of an `IllegalArgumentException`.

This fixes the problem by changing the check from `>` to `>=`, which will now return true in the given scenario.

Author: Micah Zoltu <[email protected]>

Reviewers: Ismael Juma <[email protected]>, Grant Henke <[email protected]>, Ewen Cheslack-Postava <[email protected]>

Closes apache#748 from Zoltu/patch-1
  • Loading branch information
MicahZoltu authored and ewencp committed Jan 22, 2016
1 parent 21c6cfe commit a19729f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static Schema schemaFor(Schema[][] schemas, int apiKey, int version) {
if (apiKey < 0 || apiKey > schemas.length)
throw new IllegalArgumentException("Invalid api key: " + apiKey);
Schema[] versions = schemas[apiKey];
if (version < 0 || version > versions.length)
if (version < 0 || version > latestVersion(apiKey))
throw new IllegalArgumentException("Invalid version for API key " + apiKey + ": " + version);
if (versions[version] == null)
throw new IllegalArgumentException("Unsupported version for API key " + apiKey + ": " + version);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.kafka.common.protocol;

import org.junit.Test;

public class ProtoUtilsTest {
@Test(expected = IllegalArgumentException.class)
public void schemaVersionOutOfRange() {
ProtoUtils.requestSchema(ApiKeys.PRODUCE.id, Protocol.REQUESTS[ApiKeys.PRODUCE.id].length);
}
}

0 comments on commit a19729f

Please sign in to comment.