Skip to content

Add more test for OpenAPI Specification v3.1 #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* *
* * *
* * * * Copyright 2019-2023 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package test.org.springdoc.api.v31.app8;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExamplesController {

@GetMapping(value = "/")
public ExamplesResponse index() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test.org.springdoc.api.v31.app8;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

public record ExamplesResponse(
@Schema(description = "self's user info", requiredMode = REQUIRED)
UserInfo self,
@Schema(description = "friend, deprecated, use friends instead", requiredMode = NOT_REQUIRED)
@Deprecated
UserInfo friend,
@Schema(description = "friends", requiredMode = NOT_REQUIRED)
List<UserInfo> friends
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.org.springdoc.api.v31.app8;

import io.swagger.v3.oas.annotations.media.Schema;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Schema(description = "the foo bar", deprecated = true)
public record FooBar(
@Schema(description = "foo", requiredMode = REQUIRED, examples = {"1", "2"})
int foo
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* *
* * *
* * * * Copyright 2019-2023 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package test.org.springdoc.api.v31.app8;

import test.org.springdoc.api.v31.AbstractSpringDocV31Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp8Test extends AbstractSpringDocV31Test {

@SpringBootApplication
static class SpringDocTestApp {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.org.springdoc.api.v31.app8;

import io.swagger.v3.oas.annotations.media.Schema;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Schema(description = "user info")
public record UserInfo(
@Schema(description = "The user's name", requiredMode = REQUIRED, examples = {"Madoka", "Homura"})
String name,
@Schema(description = "The user's age", requiredMode = REQUIRED, examples = {"114", "514"})
int age,
@Schema(description = "The user's fooBar", requiredMode = REQUIRED)
FooBar fooBar
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/": {
"get": {
"tags": [
"examples-controller"
],
"operationId": "index",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ExamplesResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ExamplesResponse": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ExamplesResponse": {
"ExamplesResponse": {
"type": "object",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohnNiang It seems that the current unit test also not include "type": "object"

"ExamplesResponse": {
"properties": {
"name": {
"type": "string",
"description": "name",
"examples": [
"name"
]
},
"subject": {
"type": "string",
"description": "subject",
"example":"Hello",
"examples": [
"Hello",
"World"
]
}
},
"required": [
"name",
"subject"
]
}

Copy link
Contributor Author

@xiaoxiangmoe xiaoxiangmoe Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here: https://spec.openapis.org/oas/v3.1.0#data-types

Data types in the OAS are based on the types supported by the JSON Schema Specification Draft 2020-12.

In JSON Schema Specification Draft 2020-12, "type" field is optional, so this is valid JSON schema.

And the api-gen is done by swagger-core. We can advise swagger-core to provide "type": "object".

"properties": {
"self": {
"$ref": "#/components/schemas/UserInfo",
"description": "self's user info"
},
"friend": {
"$ref": "#/components/schemas/UserInfo",
"deprecated": true,
"description": "friend, deprecated, use friends instead"
},
"friends": {
"type": "array",
"description": "friends",
"items": {
"$ref": "#/components/schemas/UserInfo"
}
}
},
"required": [
"self"
]
},
"FooBar": {
"deprecated": true,
"description": "the foo bar",
"properties": {
"foo": {
"type": "integer",
"format": "int32",
"description": "foo",
"examples": [
"1",
"2"
]
}
},
"required": [
"foo"
]
},
"UserInfo": {
"description": "user info",
"properties": {
"name": {
"type": "string",
"description": "The user's name",
"examples": [
"Madoka",
"Homura"
]
},
"age": {
"type": "integer",
"format": "int32",
"description": "The user's age",
"examples": [
"114",
"514"
]
},
"fooBar": {
"$ref": "#/components/schemas/FooBar",
"description": "The user's fooBar"
}
},
"required": [
"age",
"fooBar",
"name"
]
}
}
}
}