Defining conditional attributes in OpenAPI
I need to define a request for a searching service in JSON. Request can have either geographical coordinates (longitude and latitude) or postal code. Either one must be present. If longitude is present, then latitude is required and vice versa. If both are omitted, postal code must be present. If geo coordinates present, postal code is optional (will be ignored if present). I am having trouble defining such request in swagger editor. If someone can help me, I will appreciate it greatly. We are using v.3.0.3 and the swagger file will be used to generate client code.SolvedoneOf polymorphic Property
Hi, I require a polymorphic property in my specification. To demonstrate the requirement, I have modified the Pet example by adding a booking schema object that has a polymorphic pet property. 3.0.24 Java codegen runs without error and the generated code compiles. { "openapi": "3.0.2", "info": { "title": "Communication Model", "version": "141405445", }, "paths": { }, "components": { "schemas": { "booking": { "type": "object", "properties": { "pet": { "oneOf": [ { "$ref": "#/components/schemas/Cat" }, { "$ref": "#/components/schemas/Dog" } ], "discriminator": { "propertyName": "petType", "mapping": { "dog": "#/components/schemas/Dog", "cat": "#/components/schemas/Cat" } } }, "foo" : { "type": "string" } } }, "Pet": { "type": "object", "discriminator": { "propertyName": "petType" }, "properties": { "name": { "type": "string" }, "petType": { "type": "string" } }, "required": [ "name", "petType" ] }, "Cat": { "description": "A representation of a cat. Note that `Cat` will be used as the discriminator value.", "allOf": [ { "$ref": "#/components/schemas/Pet" }, { "type": "object", "properties": { "huntingSkill": { "type": "string", "description": "The measured skill for hunting", "default": "lazy", "enum": [ "clueless", "lazy", "adventurous", "aggressive" ] } }, "required": [ "huntingSkill" ] } ] }, "Dog": { "description": "A representation of a dog. Note that `Dog` will be used as the discriminator value.", "allOf": [ { "$ref": "#/components/schemas/Pet" }, { "type": "object", "properties": { "packSize": { "type": "integer", "format": "int32", "description": "the size of the pack the dog is from", "default": 0, "minimum": 0 } }, "required": [ "packSize" ] } ] } } } } However, I have an issue serializing and deserializing the Booking class with the pet polymorphic property. The generated Booking class looks like this: public class Booking { @JsonProperty("pet") private OneOfbookingPet pet = null; Where OneOfbookingPet is a marker interface. public interface OneOfbookingPet { } The Pet super class has the Jackson annotations. @JsonSubTypes({ @JsonSubTypes.Type(value = Cat.class, name = "Cat"), @JsonSubTypes.Type(value = Dog.class, name = "Dog"), }) public class Pet { Concrete Pet implementations extend and Pet and ImplementOneOfbookingPet. public class Dog extends Pet implements OneOfbookingPet { Serializing a Booking object results in the JSON below: {"pet":{"name":"Fido","packSize":4},"foo":"bar"} However deserializing it back into a Booking object obviously fails. The type of the pet property in Booking isOneOfbookingPet which has no Jackson annotations, so no discriminator is added to the JSON. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `domain.OneOfbookingPet` (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: (String)"{"pet":{"name":"Fido","packSize":4},"foo":"bar"}"; line: 1, column: 8] (through reference chain: domain.Booking["pet"]) Are polymorphic properties supported? What have I done wrong? Any help appreciated. Cheers, Paul2.2KViews2likes0CommentsoneOf throws ValueError: Invalid value for ... on response parsing if not matching the last referenc
Hi there! I'm using openapi 3.0.3 and I have an issue with my client: I expect an answer of the type Foo like Foo: oneOf: - $ref: "#/components/schemas/Bar1" - $ref: "#/components/schemas/Bar2" discriminator: propertyName: type mapping: bar1: "#/components/schemas/Bar1" bar2: "#/components/schemas/Bar2" And if my client receives Bar1, it will fail because in foo.py, there is a check for allowed_values = ["bar2"] Which throws: ValueError: Invalid value for `type` (bar1), must be one of ['bar2'] I tested it a bit and it looks like it's taking the last ref in oneOf (Bar2). Do you know how to fix it?How to get generate ZonedDateTime from OpenApi?
I am trying get `ZonedDateTime` in my generated code from OpenApi. There is a `If-Modified-Since` header that OpenApi generates on my endpoint but it's in the type of `LocalDateTime`. This is causing issues on my integration tests. task buildEndpointsFromYAML(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) { generatorName = "spring" inputSpec = acquirerYML outputDir = "$buildDir/generated".toString() groupId = "$project.group" id = "$project.name-java-client" version = "$project.version" apiPackage = "com.abc.xyz.apiEndpoints" modelPackage = "com.abc.xyz.apiModels" enablePostProcessFile = true skipOverwrite = false typeMappings = [ OffsetDateTime: "ZonedDateTime" ] importMappings = [ "java.time.OffsetDateTime" : "java.time.ZonedDateTime" ] configOptions = [ configPackage : "com.abc.xyz.apiEndpoints.config", java11 : "true", dateLibrary : "java8-localdatetime", serializationLibrary: "jackson", library : "spring-boot", useBeanValidation : "true", interfaceOnly : "true", serializableModel : "true", useTags : "true" ] } I've tried to some of the suggestions here too: https://stackoverflow.com/questions/56237650/use-java-time-instant-to-represent-datetime-instead-of-offsetdatetime but either it resulted in compilation errors or the type would go from `LocalDateTime` to `Object` as opposed to `ZonedDAteTime`. This is part of the swagger.yaml: parameters: - name: If-Modified-Since in: header description: If-Modified-Since required: false type: string format: date-time Thanks for any guidance!Circular references in OpenAPI
Can someone please inform me if circular references are allowed when using "allOf","oneOf" and "anyOf" keywords? The OpenAPI specification does not explicitly say anything about that. Here's an example API specification that causes all sorts of problems when attempting to generate code from it using different code generators. Essentially I'd like to combine inheritance with polymorphism so that I have an "abstract" type Animal that I can return from any method, and also Animal is never just the base class, it's always either Dog or Cat, both of which inherit some of the Animal's properties. Is this a valid schema according to OpenAPI 3 specification or not? { "openapi": "3.0.1", "info": { "title": "PET API", "version": "v1" }, "paths": { "/pets/{name}": { "get": { "responses": { "200": { "description": "Success", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Animal" } } } } } } } }, "components": { "schemas": { "Animal": { "type": "object", "oneOf": [{ "$ref": "#/components/schemas/Cat" }, { "$ref": "#/components/schemas/Dog" } ], "properties": { "name": { "type": "string", }, "animalType": { "type": "string" } }, "discriminator": { "propertyName": "animalType" } }, "Cat": { "type": "object", "allOf": [{ "$ref": "#/components/schemas/Animal" }, { "type": "object", "properties": { "whiskersCount": { "type": "integer", } } } ] }, "Dog": { "type": "object", "allOf": [{ "$ref": "#/components/schemas/Animal" }, { "type": "object", "properties": { "tailWagsPerSecond": { "type": "integer", } } } ] }, } } }2.4KViews1like0CommentsOpenAPI not encoding multipart/mixed correctly
This is how the relevant portion of my endpoint looks like: "/global-types/import-coordinates": post: summary: Import coordinates operationId: importCoordinates requestBody: content: multipart/mixed: schema: type: object properties: file: type: string format: binary attributes: type: array items: type: string identifier: type: string required: - file - attributes - identifier responses: 201: $ref: 'model.yaml#/components/responses/created' 400: $ref: 'model.yaml#/components/responses/badRequest However, when I call the generated function from TypeScript: file is not encoded properly. Chrome DevTools show it to be of type object File instead of binary The request fails with exit code 415 Unsupported Media Type I found this issue on OpenAPIs GitHub. It's very old, but still not closed. Might this be related to my issue? This is my first post to this forum, so please tell me, if you need more information. Thank you for every incoming reply or effort to help me out with this.how to have different required fields for the create (put) and update api?
I have a schema defined say star which has name as required field.But i want this field to be required in create body request but not in update body request. What is the correct way to do without redundant code? Star: title: Star description: star found required: - name properties: id: description: the id for the star type: string format: uuid4 readOnly: true x-go-name: ID name: description: name of the star type: string x-go-name: StarPolymorphic responses in Python Client Side
Code generation for the response of a method that has multiple possible accepted response schemas is broken in generated python code. I have a GET method that is supposed utilize 3.0's polymorphic capabilities by having a response that should return "oneOf" 3 types of schemas. My response looks like this: components: responses: RunResponse: description: different response types for Runs content: application/json: schema: oneOf: - $ref: '#/components/schemas/AllRun' - $ref: '#/components/schemas/IonRun' - $ref: '#/components/schemas/IllRun' But in the response type in the generated code it is looking for this: When ran with this as the response type I get an error as there is no model of "OneOfAllRunsIonRunsIllruns". How can this be fixed? Because as of right now polymorphism doesn't work with python generated code.Codegen and inheritance
I will start by saying I'm not sure how to even search for this, I tried but couldn't find a post like my problem. Currently I'm having some troubles with how to hanlde inheritance in swagger 3.0 and codegen on that swagger. The api this swagger is for uses alot of the same fields accross its different endpoints. To reduce the workload around updates to these fields I pulled all of the fields out into their own section in the schema. This has been fine for the documentation side but codegen is now generating models for each seperate field and getting really confused because of it. I have a couple of questions I'm wanting to ask here. Is this the correct way to handle repeating fields? is there a better way while not requring updated to each location the field is used? Assuming the answer to my first question isn't I'm doing everything wrong how can I reformat my swagger file to codegen only the actual models openapi: 3.0.0 info: title: Test Service version: 0.4.0 paths: /example: parameters: - $ref: '#/components/parameters/X-Request-ID' - $ref: '#/components/parameters/X-Correlation-ID' put: tags: - example summary: Update | /example PUT description: needed to hide these requestBody: required: true content: application/json: schema: $ref: '#/components/schemas/update' responses: '200': $ref: '#/components/responses/PutResponse' components: schemas: update: type: object properties: clientId: $ref: '#/components/schemas/clientId' userId: $ref: '#/components/schemas/userId' deviceId: $ref: '#/components/schemas/deviceId' state: $ref: '#/components/schemas/state' name: $ref: '#/components/schemas/name' required: - clientId - userId - deviceId putResponse: type: object properties: clientId: $ref: '#/components/schemas/clientId' userId: $ref: '#/components/schemas/userId' deviceId: $ref: '#/components/schemas/deviceId' state: $ref: '#/components/schemas/state' name: $ref: '#/components/schemas/name' lastUpdated: $ref: '#/components/schemas/lastUpdated' X-Correlation-ID: description: needed to hide these example: 2438ac3c-37eb-4902-adef-ed16b4431030 pattern: ^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$ type: string format: uuid clientId: description: needed to hide these example: 900900 pattern: ^[a-zA-Z0-9]{1,64}$ type: string deviceId: description: needed to hide these example: 7363b8ae6b2247b99f5d56fc81102254 pattern: ^[a-zA-Z0-9]{1, 32}$ type: string userId: description: needed to hide these example: meoyyd8za8jdmwfm pattern: ^[a-zA-Z0-9]{1,64}$ type: string state: type: string description: needed to hide these TRUSTED - Used to minimize friction for a user who has passed step-up authentication BANNED - Used to block access to an account for a specified device enum: - TRUSTED - BANNED name: type: string description: needed to hide these example: iPhone6sNina pattern: ^\w{1,255}$ lastUpdated: type: string format: date-time description: needed to hide these example: 2018-02-22T01:02:03.123Z pattern: "yyyy-MM-dd'T'HH:mm:ss'Z'" parameters: X-Request-ID: description: needed to hide these name: X-Request-ID schema: $ref: '#/components/schemas/X-Correlation-ID' deprecated: true in: header X-Correlation-ID: description: needed to hide these name: X-Correlation-ID schema: $ref: '#/components/schemas/X-Correlation-ID' required: true in: header clientId: name: clientId description: needed to hide these schema: $ref: '#/components/schemas/clientId' required: true in: path deviceId: name: deviceId description: needed to hide these schema: $ref: '#/components/schemas/deviceId' required: true in: path userId: name: userId description: needed to hide these schema: $ref: '#/components/schemas/userId' required: true in: path responses: PutResponse: description: needed to hide these headers: X-Correlation-ID: schema: $ref: '#/components/schemas/X-Correlation-ID' content: application/json: schema: $ref: '#/components/schemas/putResponse' This ends up generating swagger filed with models for each field like userId, deviceId, and so on. What I really want is the update and postRepsonse models.