swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.36k stars 2.17k forks source link

Return object with array of objects inside #227

Closed 4lejandrito closed 11 years ago

4lejandrito commented 11 years ago

Hi, I have a Jersey Web Service which returns a JSON object containing an array of another JSON strucutre as follows:

@ApiClass( value = "XDataAnswerRESTGetStatus", description = "This command allows to get different current status of the device")
public class XDataAnswerRESTGetStatus extends XDataAnswerREST {

    @ApiProperty( required = true, value = "List of statuses", dataType = "List[my.package.MyClass]")
    public List<MyClass> statuses = new ArrayList<MyClass>();

    ...    
}

Using Swagger-ui the class "MyClass" does not get documented. Is there any way to solve this?

Thanks in advance,

Alex

fehguy commented 11 years ago

May I suggest you switch to 1.3.0-SNAPSHOT, which allows strong-typing of the response classes and a much better model introspecter? You can grab the snapshot source from here:

https://github.com/wordnik/swagger-core/tree/develop-1.3

a java example is here:

https://github.com/wordnik/swagger-core/tree/develop-1.3/samples/java-jaxrs

and snapshots are in sonatype oss:

https://oss.sonatype.org/content/repositories/snapshots/com/wordnik/swagger-core_2.9.1/1.3.0-SNAPSHOT/

4lejandrito commented 11 years ago

Thank you very much, I will try it tomorrow.

4lejandrito commented 11 years ago

Hello again :)

I have tried to update to 1.3.0-SNAPSHOT as follows:

<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-jaxrs_2.9.1</artifactId>
    <version>1.3.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

I have changed every annotation to its new name (ApiResponses, ApiModel, etc...) but I have the following problems:

  1. Some jersey dependencies have disappeared and now the application does not start. I can solve this by adding them myself I hope, but this is a problem that has happened right after updating you dependency. Maybe I am missing something...
  2. I saw you fixed issue #52 in commit https://github.com/wordnik/swagger-core/commit/21390159913251806922d422ef27a53169c9e3bb but I am having the same issue when trying to annotate a class member with ApiParam. Am I doing something wrong? I was using a fork of swagger made by me with a workaround for this issue until you solved it in the official release. I am afraid I cannot update to 1.3.0-SNAPSHOT so far.

Nevertheless, thank you for your help. I am looking forward to seeing a 1.3.0 official release with all my needs solved :)

fehguy commented 11 years ago

Yes, jersey is now a transitive dependency, so you can update the jersey version as you like. If you don't want to do that, you can simply use the jersey-specific version. Otherwise, you'll have to include them yourself.

Can you give more detail with your ApiParam issue? I'm not quite sure I understand the issue.

4lejandrito commented 11 years ago

Some weeks ago I posted #206 issue. It happened to be related to #52 so I suppose you solved it when solving #52. If you read #206 again, I believe you will understand my problems.

Thanks!!

PS: What do you mean with: jersey-specific version?

fehguy commented 11 years ago

Are you sure there's an issue still? I just added this test:

https://github.com/wordnik/swagger-core/commit/29034cc6c5092389900245c6b50de2351f27c764

Against the current code, having a @PathParam in the class-level and it passes fine, per #52. Perhaps you have it configured differently?

swagger-jaxrs uses Jersey as a provided dependency so you technically don't have to use it (although most people do). There is one that is bound to jersey here:

https://github.com/wordnik/swagger-core/tree/develop-1.3/modules/swagger-jersey-jaxrs

which has some additional jersey-specific features, like supporting file uploads, etc.

4lejandrito commented 11 years ago

HI, I already knew about that test, but I do not see why it is passing.

I have a field annotated with @ApiParam but it does not compile because of the annotation's target. In the source file from ApiParam:

https://github.com/wordnik/swagger-core/blob/develop-1.3/modules/swagger-annotations/src/main/java/com/wordnik/swagger/annotations/ApiParam.java

I can see that the annotation's target is still ElementType.PARAMETER. If you see the commit I made in my fork to workaround the problem (0f1501e307df219ab7bfa921400e178b6d89b87d) I had to change the annotation's target to {ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD} (maybe METHOD does not make much sense, but FIELD is the important value).

I must be misunderstanding something.

I would appreciate your help.

fehguy commented 11 years ago

I will do another test with this as a Java resource, there could be some scala magic happening. You can look at it as well to see if it's in-line with your code as well.

fehguy commented 11 years ago

Well why didn't you say it was that easy! Fixed, it was the case that scala + java were handling the field differently. A new snapshot will be uploaded shortly.

4lejandrito commented 11 years ago

Yes, It works now!!, but...

I would not say this issue is solved... You have completely solved #52 but I am afraid #227 is not.

As I said in the first message, when swagger-uing a resource like this:

@ApiModel( value = "XDataAnswerRESTGetStatus", description = "This command allows to get different current status of the device")
public class XDataAnswerRESTGetStatus extends XDataAnswerREST {

    @ApiModelProperty( required = true, value = "List of statuses", dataType = "List[my.package.MyClass]")
    public List<MyClass> statuses = new ArrayList<MyClass>();

    ...    
}

I can see something like this:

XDataAnswerRESTGetStatus {
    statuses (List[MyClass], optional):List of statuses
}

But MyClass is not documented below as I expected.

:(