I went to this swagger editor and looked into their example.
I wanted to try extending a model.
In the example there, they have:
Tag:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
xml:
name: "Tag"
So I added my Tag2:
Tag2:
allOf:
- $ref: '#/definitions/Tag'
- properties:
prop:
type: "string"
My new model does appear in the Models List, but instead of having the 2 schema names (Tag and Tag2), I have two Tag entries. One of them correctly has the new 'prop' field.
How can I make the second one to appear with the correct name?
This is a bug in Swagger Editor. It's being tracked in:
https://github.com/swagger-api/swagger-editor/issues/1288
and is caused by
https://github.com/swagger-api/swagger-js/issues/1024
Related
Let's assume I have this definition in my openapi.yml (this is just a madeup example to depict the issue I have):
components:
schemas:
Address:
type: object
properties:
name:
type: string
zip:
type: integer
format: int64
town:
type: string
This will lead to the generation of code for the model that looks like this:
public class Address {
#JsonProperty("name")
private String name = null;
#JsonProperty("zip")
private Long zip = null;
#JsonProperty("town")
private String town = null;
...
My issue is that I have to persist this Pojo in a database and there exists no table Address (let's assume it is called Places instead) and the column for the zipcode is called zipcode instead of zip.
So I would need two things:
a way to tell OpenAPI about the aliases Address=Places and zip=zipcode.
a way to make this information change the code created. For Hibernate for example this would mean to add #Table(name="Places") and #Column(name="zipcode") in the right places.
Important remark: I can not change the API, it is expected to stick to Address and zip.
Can this be done ? I checked the specs and both swagger-codegen and openapi-generator (I'ld prefer the latter) but found no sign that something like this is covered.
For openapi-generator I had a look at the Mustache templates and as far as I can tell there is no code that refers any "alias information" that was defined in the yaml file.
Do I have to go the hard way and define an OpenAPI extension like this or this plus customize the templates all by myself ?
The closest I could find is this existing extension, that defines one type of alias. But this satisfies only the first part of my demand.
Use $ref
components:
schemas:
Address:
type: object
properties:
name:
type: string
zip:
type: integer
format: int64
town:
type: string
Places:
$ref: '#/components/schemas/Address'
I'd like to outline a multipart form request body, with some fields which are required, and other fields which are optional. Generally, in OAS Schema Objects, all properties not explicitly marked with required: true are defaulted to optional. However, when outlining a requestBody with multipart/form-data content, this seems to go by the wayside, and all fields are required.
I have tried multiple ways of designating the fields required vs optional. I receive compilation errors when trying to explicitly designate a field as optional with required: false.
OAS3 spec:
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
required:
- foo
properties:
foo:
type: string
format: binary
bar:
type: string
format: binary
Expected: Detail a multipart/form-data requestBody with some fields required and some optional.
In the code example above, foo should be a required file, while bar should be an optional file.
You can add an array of required fields, for example, if you'd like do make just foo required, should be like that:
requestBody:
content:
multipart/form-data:
schema:
type: object
required: [foo]
properties:
foo:
type: string
format: binary
bar:
type: string
format: binary
If you'd like to add more than one, just add the name on the array, such as [foo,bar].
I created a custom validation annotation which is similar to #javax.validation.constraints.Pattern. For #Pattern, swagger generates a nice schema like this:
summary: my summary
operationId: my_operation_id
parameters:
- name: myParam
in: header
required: true
schema:
maxLength: 30
pattern: ^\w+$ <--- THIS ONE
type: string
But for my annotation #CustomPattern (let's say), it doesn't! The generated swagger.yml does not have pattern key under schema. How can I create one?
This is in Micronaut.
I needed to have swagger.yml generated for my controller methods' parameters.
Just stick in a #io.swagger.v3.oas.annotations.media.Schema annotation with pattern attribute. Or if it's a List, then you could use #io.swagger.v3.oas.annotations.media.ArraySchema(schema = #Schema(pattern = ?))
That will do.
How declare a 'List' in an OpenAPI/Swagger-file?
The below code has generated List<String>
myTestProperty:
type: array
items:
type: string
But not sure how to generate List<MyOwnType>.
Is there a way in OpenAPI to accomplish it? The resulting pojo must have that field.
Thank you.
My model definition looks like this:
swagger: '2.0'
definitions:
Foo:
type: object
properties:
fooProp1:
type: string
fooProp2:
type: array
items:
$ref: '#/definitions/Bar'
Bar:
type: object
properties:
blah:
type: string
I used Swagger Codegen CLI 2.3.1 to generate both Java models for backend and TypeScript models for frontend. In both cases, in the Foo class, there is only the property "fooProp1", but no "fooProp2". I expected something like
private List<Bar> fooProp2;
would be in generated Java model classes.
What did I miss? Is there any problem in my Swagger code?? How should I modify my Swagger code to get my expected server/client model code?
Many thanks in advance!!