[JAVA] [JAX-RS] Server InvalidDefinitionException: Cannot construct instance by using oneOf
Hi Smartbear Community, I have the following swagger.yaml that uses theoneOfkeyword and I used the latest Swagger codegen tools (3.0.22) to create a server stub for JAX-RS Jersey. openapi: 3.0.0 info: version: 0.0.1 title: Polymorphism example description: '' paths: /animal: post: summary: Create a new animal requestBody: required: true content: application/json: schema: $ref: '#/components/schemas/Animal' responses: '201': description: Created components: schemas: Animal: properties: name: type: string props: oneOf: - $ref: '#/components/schemas/Fish' - $ref: '#/components/schemas/Dog' Fish: type: object properties: depth: type: integer Dog: type: object properties: country: type: string I am trying to post the following example object: { "name":"Foo", "props":{ "country":"Bar" } } But this results in the following exception: javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.swagger.model.OneOfAnimalProps` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 30] (through reference chain: io.swagger.model.Animal["props"]) at org.glassfish.jersey.servlet.WebComponent.serviceImpl (WebComponent.java:432) at org.glassfish.jersey.servlet.WebComponent.service (WebComponent.java:370) at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:389) at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:342) at org.glassfish.jersey.servlet.ServletContainer.service (ServletContainer.java:229) at org.eclipse.jetty.servlet.ServletHolder.handle (ServletHolder.java:808) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter (ServletHandler.java:1669) at io.swagger.api.ApiOriginFilter.doFilter (ApiOriginFilter.java:15) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter (ServletHandler.java:1652) at org.eclipse.jetty.servlet.ServletHandler.doHandle (ServletHandler.java:585) at org.eclipse.jetty.server.handler.ScopedHandler.handle (ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle (SecurityHandler.java:577) at org.eclipse.jetty.server.session.SessionHandler.doHandle (SessionHandler.java:223) at org.eclipse.jetty.server.handler.ContextHandler.doHandle (ContextHandler.java:1127) at org.eclipse.jetty.servlet.ServletHandler.doScope (ServletHandler.java:515) at org.eclipse.jetty.server.session.SessionHandler.doScope (SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope (ContextHandler.java:1061) at org.eclipse.jetty.server.handler.ScopedHandler.handle (ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle (ContextHandlerCollection.java:215) at org.eclipse.jetty.server.handler.HandlerCollection.handle (HandlerCollection.java:110) at org.eclipse.jetty.server.handler.HandlerWrapper.handle (HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle (Server.java:497) at org.eclipse.jetty.server.HttpChannel.handle (HttpChannel.java:310) at org.eclipse.jetty.server.HttpConnection.onFillable (HttpConnection.java:257) at org.eclipse.jetty.io.AbstractConnection$2.run (AbstractConnection.java:540) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob (QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run (QueuedThreadPool.java:555) at java.lang.Thread.run (Thread.java:748) Swagger-codegen version Swagger-codegen version: 3.0.22 Command line used for generation CLI command:java -jar swagger-codegen-cli.jar generate -i openapi.yaml -l jaxrs-jersey -o jaxrs-jersey/ Any advise how to fix this issue is much appreciated!1.6KViews0likes4CommentsWhat does <*> symbol represent in json swagger?
I have a question in JSON swagger. May I know what does <*> symbol represent in JSON swagger? This is how the format looks like after I import the JSON swagger into the online editor. What does <*> actually mean? CrossCurrencyRequest{ items* { < * >: CurrencyPairRequest{ amount* number currencyPair* string decimalPlace integer($int32) } } } Below are the definitions from the swagger file. definitions: CrossCurrencyRequest: type: object required: - items properties: items: type: object additionalProperties: $ref: '#/definitions/CurrencyPairRequest' title: CrossCurrencyRequest I have the sample request as given below. How to make the request generates the "key1", "key2", etc? As it seems like not a predefined object from the swagger file. Sample request: { "items": { "key1": { "amount": 1000.123456789, "currencyPair": "USD-SGD", "decimalPlace": 5 }, "key2": { "amount": 1000.12345, "currencyPair": "USD-IDR" }, "key3": { "amount": 1000.12345, "currencyPair": "USD-ZNH" } } }SolvedHow do I properly define an example for a nullable property in OpenAPI 3?
I am using OpenAPI and openapi-generator-maven-plugin with Spring Boot. I'm trying to create an example object in my response. I've followed the suggestions of several different web pages, especially this one: https://swagger.io/docs/specification/adding-examples/ But no matter what I try, I can't get the example data to show up correctly. Here the object that should be in a request: Period: type: object required: - startDate - endDate properties: startDate: type: string format: date nullable: true example: "2021-01-01" endDate: type: string format: date nullable: true example: "2030-01-01" CreateInstancesRequest: type: object required: - id properties: id: $ref: '#/components/schemas/Id' period: allOf: - $ref: '#/components/schemas/Period' But I get not what I want: I expect something like this: { "id": "some-id", "period": { "endDate": "2020-01-01", "startDate": "2022-01-01" } } But maybe I am wrong because generated java code for the Period class: I thinkpresentproperty from actual example came fromorg.openapitools.jackson.nullable.JsonNullableclass. Can you help me, please?Java JAXB generated classes and swagger
I have JAXB generated classes @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "MyResponse", propOrder = { "member1", "member2", "member3" }) public class MyResponse implements Serializable { <... other class members; setters/ getters ...> } @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "myRequest", propOrder = { "test1", "test2", "test3" }) public class MyRequest implements Serializable { <... other class members; setters/ getters ...> } However, swagger 2 (version 3.0.0) generates example values as <?xml version="1.0" encoding="UTF-8"?> <JAXBElement«MyRequest»> <name> <localPart>jhgkhgk</localPart> <namespaceURI>gfjgfj</namespaceURI> <prefix>gfjhgfj</prefix> </name> <nil>true</nil> <MyRequest> <test1>string</test1> <test2>string</test2> <test3>string</test3> </MyRequest> </JAXBElement«MyRequest»> How to force swagger to generate example values of <MyRequest> <test1>string</test1> <test2>string</test2> <test3>string</test3> </MyRequest> My Controller class public @ResponseBody JAXBElement<MyRequest> test( @RequestBody JAXBElement<MyResponse> message) { }597Views0likes0CommentsAdd Upload button for OpenAPI 2.0 file as Octet-Stream
Context I am using OpenAPI ver 2.0 I have API which consumesapplication/octet-stream(JavaInputStream) Issue In OpenAPI ver 2.0 I can only set input type asmultipart/form-data(link) to have upload button. When I have applied this solution for my swagger: <code> "consumes": [ "multipart/form-data" ], "produces": [ "application/json" ], "parameters": [ { "in": "formData", "name": "messageFile", "description": "Description...", "required": true, "type": "file" } ] </code> I am getting following error:Unsupported Media Typeafter sending a file - itis normal due to my API wants octet-stream. Question Is it possible to have upload button using OpenAPI ver 2.0 andapplication/octet-streamlike: If no, then what will be better solution / workaround for this?Solved3.6KViews0likes3CommentsOne Endpoint, multiple examples/Responses? (Java Annotations)
Hey, I'm using Swagger to document my Java API. This is using a Jersey resources/Jackson JSON objects and I want to document it using annotations in the code. The 3 swagger libraries I'm using are: swagger-core, swagger-jaxrs2, swagger-models. The issue I keep running into is that I sometimes want to document different implementations for an endpoint but I'm not sure how to go about it. To give a straigthforward example, I have the endpoint "/login" which takes the Jackson/POJO object "LoginAuthJson". The issue is that in this case a login can be initiated either by a Facebook user (where the object will contain a facebook ID and a token from facebook), or it can be a login from a registered email user (in which case the body contains an email and a password.). So I want to somehow be able to show example bodies for both facebook and email login separately. Any suggestion on how I can do something like this? Edit: so I have gotten the schema to work sort of like I Want using oneOf, but that doesn't seem to work for examples 😞634Views0likes0CommentsJava code gen "incompatible types" generic list value
I am getting an error in java code gen for a map of String->String[] in java. The swagger source definition: DataMap: description: Provides a generic map of string->string additionalProperties: $ref: '#/components/schemas/StringArray' title: DataMap type: object StringArray: description: String array items: type: string title: StringArray type: array Results in java code: private Map<String, List<String>> dataMap = null; public RequestMetaData putDataMapItem(String key, List<String> dataMapItem) { if (this.dataMap == null) { this.dataMap = new HashMap<String, List>(); } this.dataMap.put(key, dataMapItem); return this; } This does not compile. It fails with error: incompatible types: HashMap<String,List> cannot be converted to Map<String,List<String>> This seems like a common definition structure. Has anyone seen this/resolved it?Varied Example Values from same Model (Java)
Hey, I have two (somewhat related) situations where I would love some input on how to best approach this. I am using the swagger-core java library and doing all of this with annotations to my code. 1) I am aiming for an API response which has an Outside wrapper with the status, and a result document with the key for the specific document you are fetching (example here: https://medium.com/@shazow/how-i-design-json-api-responses-71900f00f2db ). One problem I have encountered here is that if I have 1 "ResultWrapper" model which has one "Result" model, it would (in examples and the model) always display all possible results. So what I have ended up doing is created A Wrapper and a Result model for each possible result (f.ex a UserWrapper and userResult) which just extend the Abstract result wrapper and adds the User specific fields: https://www.dropbox.com/s/tsumo4sz8iot8b5/Screenshot%202020-04-21%2012.56.44.png?raw=1 2) In a similar vein, I have defined the "example" values over the relevant fields (so for examle inside the User result model). The problem I'm having here is that it always returns all the fields as example values. For example one request I have (which would still use the User model), only returns an ID and activation status, but because I just define the User model as the response, it uses the example values of that field. The one way I can see to tackle this is to create extensions of the models with different example values, descriptions etc. But that then comes with the problem that the bottom "Models" section has a ton of duplicated models, because each User response would have its own model (this is already kind of happening where each result has 3 different models because of the nested nature.) Any input on how to best approach this? I guess to summarise I want to use the same underlying models for a lot of different operations (POST and responses), but use different example values for the different requests, and I want to avoid bloating (specifically the Model section) with endless configurations of the same model.582Views0likes0CommentsSupport validation groups when reading Java annotations
Hi, I've got a Java Spring Boot project using swagger core v3 version 2.1.1 and springdoc openapi core version 1.1.49. We're using swagger annotations on our POJOs to generate our documentation, which is very useful - each field has a@Schema annotation, and swagger automatically reads the validation annotations (@NotNull, @Size, etc) to incorporate them into the documentation. My question deals with those validation annotations. In some cases, we're using validation groups (eg, @NotNull(groups = Create.class) ) to enable/disable annotations based on which endpoint is being used (eg, a field required to be Not Null on a POST might be allowed to be null on a PATCH.) Is there a way to get swagger to automatically detect those differences and populate them to the documentation? Currently, it's just reading the annotations and ignoring the validation groups. In the alternative, is it possible to override the automatically-detected annotations in-code? EDIT: The other use case we're hitting is trying to make certain fields hidden for some endpoints, but not for others. Is there a way to update the default implementation schema in that way?1.2KViews0likes0CommentsGuice + Undertow + Pippo + Swagger
I have a basic Java CRUD app which uses Guice as DI and Undertow as the base server(Pippo is the web framework I'm using). How do I integrate Swagger to this? I could not find a relevant example athttps://github.com/swagger-api/swagger-samples. Can you please point me towards a basic example if there is existing support for my use case?721Views0likes0Comments