Swagger alias type?

812 views
Skip to first unread message

anton kropp

unread,
Mar 2, 2015, 9:37:09 PM3/2/15
to swagger-sw...@googlegroups.com
I'm creating value type wrappers in my java application such that I don't access primitives in my dropwizard resources.  I'd like to be able to tell swagger to document them as if they were primitives in both the swagger-ui and in the api-docs. I've tried registering a typeconverter with the full class name of my value wrapper and the target primitive name, but it doesn't seem to show up in either the api-docs or the swagger-ui. I'm using 

<dependency>
   
<groupId>com.wordnik</groupId>
   
<artifactId>swagger-jaxrs_2.10</artifactId>
   
<version>1.3.10</version>
</dependency>


 
<dependency>
   
<groupId>com.wordnik</groupId>
   
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
   
<version>1.3.10</version>
</dependency>

Any help would be great. Thanks!

Ron Ratovsky

unread,
Mar 2, 2015, 10:05:17 PM3/2/15
to swagger-sw...@googlegroups.com
Can you share your converters?

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

anton kropp

unread,
Mar 3, 2015, 2:44:04 PM3/3/15
to swagger-sw...@googlegroups.com
Ron, I have things set up liek this:

final TypeConverter typeConverter = new TypeConverter();


typeConverter
.add(TestValueWrapper.class.getName(), String.class.getName());


ModelConverters.addConverter(typeConverter, true);ModelConverters.addConverter(typeConverter, true);


So my test value wrapper is a reference type that literally just boxes a string. I'd like for whever that class is encountered for it to say that it accepts a string primitive, vs the class type.  


On Monday, March 2, 2015 at 7:05:17 PM UTC-8, Ron wrote:
Can you share your converters?
On Mon, Mar 2, 2015 at 9:37 PM, anton kropp <akr...@gmail.com> wrote:
I'm creating value type wrappers in my java application such that I don't access primitives in my dropwizard resources.  I'd like to be able to tell swagger to document them as if they were primitives in both the swagger-ui and in the api-docs. I've tried registering a typeconverter with the full class name of my value wrapper and the target primitive name, but it doesn't seem to show up in either the api-docs or the swagger-ui. I'm using 

<dependency>
   
<groupId>com.wordnik</groupId>
   
<artifactId>swagger-jaxrs_2.10</artifactId>
   
<version>1.3.10</version>
</dependency>


 
<dependency>
   
<groupId>com.wordnik</groupId>
   
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
   
<version>1.3.10</version>
</dependency>

Any help would be great. Thanks!

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

anton kropp

unread,
Mar 3, 2015, 2:44:38 PM3/3/15
to swagger-sw...@googlegroups.com
Obviously minus that extra copy pasta fail at the end

anton kropp

unread,
Mar 12, 2015, 4:02:23 PM3/12/15
to swagger-sw...@googlegroups.com
Any ideas? Would love to get this resolved 

tony tam

unread,
Mar 12, 2015, 7:09:49 PM3/12/15
to swagger-sw...@googlegroups.com
Hi Anton, Just getting to this now.  I think to help I'd need to see more about the model converter.  What can you share?  There are tests that show how to create a model converter, and a wiki page here:

anton kropp

unread,
Mar 12, 2015, 9:17:03 PM3/12/15
to swagger-sw...@googlegroups.com
Ok, I see the issue here. The TypeConverter changes the model name type when the model is contained in another object. For example, if I have a class 

class Person{
   
FirstName firstName;
   
LastName lastName;
}

Then if I say 

TypeConverter converter = new TypeConverter();
converter
.add("FirstName", "string");
ModelConverters.addConverter(converter, true);


Then the person model will show the first name as a string to any object that contains that type in them.

I also can register an override converter that changes any reference to "FirstName" with a custom json string representing what it is if I set the Id to be the short class name and give it some properties. so when someone looks to see what a FirstName is they can get a json kind of like:

{  
   
"id":"FirstName",
   
"properties":{  
     
"value":{  
         
"type":"string"
     
}
   
}
}


  But there doesn't seem to be a way to just treat FirstName as a string without making it some sort of object.  I want anyone who is passing in a string as a path param to not even know it maps to a FirstName object since FirstName is a value type wrapper for me (its an immutable data class that wraps a single primitive). 

Is there any way to just forgoe the entire fact that its a class and just treat FirstName like a string for all intents and purposes?

Thanks!

p.s. if not, what I have is good enough for now

tony tam

unread,
Mar 12, 2015, 9:57:56 PM3/12/15
to swagger-sw...@googlegroups.com
Try this:

override def typeMap = Map("Firstname" -> "String")

That tells swagger to use "string" for first name.  I *think* it's proper case but might be "string"

Post back after trying!  And maybe you can help enhance the wiki page with more info on doing this.

anton kropp

unread,
Mar 13, 2015, 1:47:05 PM3/13/15
to swagger-sw...@googlegroups.com
Isn't the type converter already doing that when I register the class simpleName with it? 

I'm registering two converters, one is a model converter which tells swagger that the fully qualified name should be shown with a particular json string when showng the model info, and then the type converter to say that the simple name should show as a string.  

With the model converter I get the json i posted below that shows that the element contains a string.  With the override converter I do get that values within another object are a string. For example:

{
 
"id": "Person",
 
"properties": {
   
"firstName": {
     
"type": "string"
   
}
 
}
}


Which is great. However, if I have an api that takes a firstname as a path parameter its still going to say that the type is of FirstName:

[
 
{
   
"name": "firstName",
   
"required": true,
   
"type": "FirstName",
   
"paramType": "path",
   
"allowMultiple": false
 
}
]

So I think the typemap is working as expected, but I want that in the path param api that it shows up as the primitive type and not the reference type.  Not sure if I misunderstood your suggestion or not though. I appreciate the help!

-anton

tony tam

unread,
Mar 13, 2015, 1:49:22 PM3/13/15
to swagger-sw...@googlegroups.com
Hmm, I don't know if that use case is implemented.  I think you should open a ticket on swagger-core for this

tony tam

unread,
Mar 13, 2015, 1:53:07 PM3/13/15
to swagger-sw...@googlegroups.com
Also... you do know that there's a new version of swagger that is pure java?  See here:


there's a dropwizard sample in the samples folder as well.

anton kropp

unread,
Mar 13, 2015, 1:56:47 PM3/13/15
to swagger-sw...@googlegroups.com
Neat, I'l open a ticket for this use case. Thanks

anton kropp

unread,
Mar 13, 2015, 2:04:20 PM3/13/15
to swagger-sw...@googlegroups.com

btiernay

unread,
May 8, 2016, 8:28:28 AM5/8/16
to Swagger
I'm having the exact same issue using Swagger 1.3 with Dropwizard 0.6.2. However, I'm just trying to map to string in a query parameter, not a path parameter. Perhaps this is the same case. Curious if there is now a way to workaround this? I tried implementing the same setup and failed.

Cheers,

Bob

Ron Ratovsky

unread,
May 9, 2016, 11:14:59 AM5/9/16
to swagger-sw...@googlegroups.com
You’re using a really old version. Are you able to upgrade?

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages