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

Only classes in com.wordnik are properly emitted in model #46

Closed
geeksville opened this issue Jul 2, 2012 · 7 comments
Closed

Only classes in com.wordnik are properly emitted in model #46

geeksville opened this issue Jul 2, 2012 · 7 comments
Milestone

Comments

@geeksville
Copy link

Hi,

I'm using your super cool library and I've noticed a small problem. The TypeUtil class filters emitted model classes to com.wordnik.*. This is undoubtably done to avoid emitting lots of uninteresting classes in java foo etc... And I agree that this sort of 'whitelist' approach is better than a backlist. Unfortunately I do have some interesting classes which I'd like to have properly emitted when they are referenced from other model objects.

How do you recommend doing this? TypeUtil is a singleton kinda deep in the bowels. My first thought was to make a new public var of whitelistPackages, and default that var to "com.wordnik.". This would allow 'advanced' users to change that should they so wish. If this is fine with you I'll make such a change and send a pull request. For reference, here's my current hackish way of making things go:

git diff modules/swagger-core/src/main/scala/com/wordnik/swagger/core/util/TypeUtil.scala
diff --git a/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/util/TypeUtil.scala b/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/u
index 8d66f5b..f75ea09 100644
--- a/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/util/TypeUtil.scala
+++ b/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/util/TypeUtil.scala
@@ -117,10 +117,15 @@ object TypeUtil {
   private def checkAndAddConcreteObjectType(classType:Type, list: java.util.List[String]) {
     if (classType.getClass.isAssignableFrom(classOf[Class[_]])){
       val listType: Class[_] = classType.asInstanceOf[Class[_]]
-      if (listType.getName.startsWith(WORDNIK_PACKAGES)) list.add(listType.getName)
+      if (isInterestingPackage(listType.getName)) list.add(listType.getName)
     }
   }
 
+  private def isInterestingPackage(name: String) = {
+    val prefixes = Seq(WORDNIK_PACKAGES, "com.xeralux.")
+    prefixes.exists { p => name.startsWith(p) }
+  }
+
   /**
    * Get all classes references by a given list of classes. This includes types of method params and fields
    */
@@ -164,7 +169,7 @@ object TypeUtil {
                 }
                 case _ =>
               }
-              if (fieldClass.startsWith(WORDNIK_PACKAGES)) {
+              if (isInterestingPackage(fieldClass)) {
                 referencedClasses.add(fieldClass)
               }
               else {
@@ -185,7 +190,7 @@ object TypeUtil {
                 }
                 case _ =>
               }
-              if (methodReturnClass.startsWith(WORDNIK_PACKAGES)) {
+              if (isInterestingPackage(methodReturnClass)) {
                 referencedClasses.add(methodReturnClass)
               }
               else {
@fehguy
Copy link
Contributor

fehguy commented Jul 4, 2012

I didn't realize this was still in the core! Looking into alternatives to it and will get back to you soon.

@ayush
Copy link
Contributor

ayush commented Jul 5, 2012

Yikes! Thanks for reporting this.

@geeksville
Copy link
Author

btw - here's a possible fix if you want it. Worked well for my case: Xeralux@8dac1cf

@ayush
Copy link
Contributor

ayush commented Jul 5, 2012

@geeksville that'll work but we probably want to externalize this to make it configurable in swagger.

@ghost ghost assigned ayush Jul 9, 2012
@webron
Copy link
Contributor

webron commented Jul 12, 2012

Opened issue 51 which I believe is related to this issue and can possibly resolve this issue along the way.

@fehguy
Copy link
Contributor

fehguy commented Aug 1, 2012

This is addressed in the 1.1-spec branch:

https://github.com/wordnik/swagger-core/tree/1.1.0-spec

You can configure the package in you web.xml through an init-param:

<init-param>
   <param-name>api.model.packages</param-name>
   <param-value>com.your.project</param-value>
</init-param>

This isn't typically needed--there is a default exclusion criteria which passes all the unit tests in SpecReaderTest.scala. But if you want to only include a certain package, please use the web.xml param per above

@fehguy
Copy link
Contributor

fehguy commented Aug 22, 2012

pushed to master and released in 1.1.0. You can take the default filtering which is sensible--or put your own in web.xml or via programmatic configuration in the TypeUtil:

https://github.com/wordnik/swagger-core/blob/master/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/util/TypeUtil.scala#L224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants