You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/09/15 17:35:41 UTC

[juneau] branch master updated: Fix bug when both parent and child classes have examples.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 84da0e4  Fix bug when both parent and child classes have examples.
84da0e4 is described below

commit 84da0e4cf8f2e7762dee0a2a1b8480bf40452d61
Author: JamesBognar <ja...@apache.org>
AuthorDate: Sat Sep 15 13:35:29 2018 -0400

    Fix bug when both parent and child classes have examples.
---
 .../juneau/jsonschema/JsonSchemaGeneratorTest.java | 24 ++++++++++++++++++++++
 .../src/main/java/org/apache/juneau/ClassMeta.java |  6 ++++++
 2 files changed, 30 insertions(+)

diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/jsonschema/JsonSchemaGeneratorTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/jsonschema/JsonSchemaGeneratorTest.java
index 3e478b0..1deb210 100755
--- a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/jsonschema/JsonSchemaGeneratorTest.java
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/jsonschema/JsonSchemaGeneratorTest.java
@@ -328,6 +328,30 @@ public class JsonSchemaGeneratorTest {
 	}
 
 	@Test
+	public void addExample_BEAN_exampleMethodOverridden_wDefault() throws Exception {
+		SimpleBeanWithExampleMethod2 b = new SimpleBeanWithExampleMethod2();
+		b.f1 = "baz";
+		JsonSchemaGeneratorSession s = JsonSchemaGenerator.DEFAULT.builder().addExamplesTo("bean").example(SimpleBeanWithExampleMethod2.class, b).build().createSession();
+		assertObjectEquals("{type:'object',properties:{f1:{type:'string'}},'x-example':{f1:'baz'}}", s.getSchema(SimpleBeanWithExampleMethod2.class));
+	}
+
+	@Test
+	public void addExample_BEAN_exampleMethodOverridden_array2d() throws Exception {
+		JsonSchemaGeneratorSession s = JsonSchemaGenerator.DEFAULT.builder().addExamplesTo("bean").build().createSession();
+		assertObjectEquals("{type:'array',items:{type:'array',items:{type:'object',properties:{f1:{type:'string'}},'x-example':{f1:'foobar'}}}}", s.getSchema(SimpleBeanWithExampleMethod2[][].class));
+	}
+
+	public static class SimpleBeanWithExampleMethod2 extends SimpleBeanWithExampleMethod {
+
+		@Example
+		public static SimpleBeanWithExampleMethod2 example2() {
+			SimpleBeanWithExampleMethod2 ex = new SimpleBeanWithExampleMethod2();
+			ex.f1 = "foobar";
+			return ex;
+		}
+	}
+
+	@Test
 	public void addExample_BEAN_exampleField() throws Exception {
 		JsonSchemaGeneratorSession s = JsonSchemaGenerator.DEFAULT.builder().addExamplesTo("bean").build().createSession();
 		assertObjectEquals("{type:'object',properties:{f1:{type:'string'}},'x-example':{f1:'foobar'}}", s.getSchema(SimpleBeanWithExampleField.class));
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index cf727c7..135813b 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -510,6 +510,9 @@ public final class ClassMeta<T> implements Type {
 					setAccessible(f, false);
 					namePropertyMethod = new Setter.FieldSetter(f);
 				}
+			}
+
+			for (Field f : c.getDeclaredFields()) {
 				if (f.isAnnotationPresent(Example.class)) {
 					if (! (isStatic(f) && isParentClass(innerClass, f.getType())))
 						throw new ClassMetaRuntimeException("@Example used on invalid field ''{0}''", f);
@@ -532,6 +535,9 @@ public final class ClassMeta<T> implements Type {
 					setAccessible(m, false);
 					namePropertyMethod = new Setter.MethodSetter(m);
 				}
+			}
+
+			for (Method m : c.getDeclaredMethods()) {
 				if (m.isAnnotationPresent(Example.class)) {
 					if (! (isStatic(m) && hasFuzzyArgs(m, BeanSession.class) && isParentClass(innerClass, m.getReturnType())))
 						throw new ClassMetaRuntimeException("@Example used on invalid method ''{0}''", m);