Skip to content

Fix arrayschema #2403

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 2 commits 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
Expand Up @@ -354,58 +354,63 @@ private static void setExamples(MediaType mediaType, ExampleObject[] examples) {
private static MediaType getMediaType(Schema schema, Components components, JsonView jsonViewAnnotation,
io.swagger.v3.oas.annotations.media.Content annotationContent, boolean openapi31) {
MediaType mediaType = new MediaType();
if (!annotationContent.schema().hidden()) {
if (components != null) {
try {
getSchema(annotationContent, components, jsonViewAnnotation, openapi31).ifPresent(mediaType::setSchema);
if (annotationContent.schemaProperties().length > 0) {
if (mediaType.getSchema() == null) {
mediaType.schema(new Schema<Object>().type("object"));
}
Schema oSchema = mediaType.getSchema();
for (SchemaProperty sp : annotationContent.schemaProperties()) {
Class<?> schemaImplementation = sp.schema().implementation();
boolean isArray = isArray(annotationContent);
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation, openapi31)
.ifPresent(s -> {
if ("array".equals(oSchema.getType())) {
oSchema.getItems().addProperty(sp.name(), s);
}
else {
oSchema.addProperty(sp.name(), s);
}
});

if (annotationContent.schema().hidden()) {
return mediaType;
}
if (components == null) {
mediaType.setSchema(schema);
return mediaType;
}
try {
getSchema(annotationContent, components, jsonViewAnnotation, openapi31).ifPresent(mediaType::setSchema);
if (annotationContent.schemaProperties().length > 0) {
if (mediaType.getSchema() == null) {
mediaType.schema(new Schema<Object>().type("object"));
}
Schema oSchema = mediaType.getSchema();
for (SchemaProperty sp : annotationContent.schemaProperties()) {
Class<?> schemaImplementation = sp.schema().implementation();
boolean isArray = false;
if (schemaImplementation == Void.class) {
schemaImplementation = sp.array().schema().implementation();
if (schemaImplementation != Void.class) {
isArray = true;
}
}
if (
hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) &&
mediaType.getSchema() != null &&
!Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) &&
!Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation, openapi31)
.ifPresent(s -> {
if ("array".equals(mediaType.getSchema().getType())) {
mediaType.getSchema().getItems().additionalProperties(s);
}
else {
mediaType.getSchema().additionalProperties(s);
}
}
);
}
}
catch (Exception e) {
if (isArray(annotationContent))
mediaType.setSchema(new ArraySchema().items(new StringSchema()));
else
mediaType.setSchema(new StringSchema());
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation, openapi31)
.ifPresent(s -> {
if ("array".equals(oSchema.getType())) {
oSchema.getItems().addProperty(sp.name(), s);
}
else {
oSchema.addProperty(sp.name(), s);
}
});
}
}
else {
mediaType.setSchema(schema);
if (
hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) &&
mediaType.getSchema() != null &&
!Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) &&
!Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation, openapi31)
.ifPresent(s -> {
if ("array".equals(mediaType.getSchema().getType())) {
mediaType.getSchema().getItems().additionalProperties(s);
}
else {
mediaType.getSchema().additionalProperties(s);
}
}
);
}
}
catch (Exception e) {
if (isArray(annotationContent))
mediaType.setSchema(new ArraySchema().items(new StringSchema()));
else
mediaType.setSchema(new StringSchema());
}
return mediaType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* *
* * *
* * * *
* * * * * 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.v30.app211;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

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

@RestController
public class HelloController {
@ApiResponses(value = @ApiResponse(
responseCode = "200",
content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schemaProperties = {
@SchemaProperty(
name = "items",
array = @ArraySchema(schema = @Schema(implementation = PagedObject.class))),
@SchemaProperty(name = "paging", schema = @Schema(implementation = Paging.class))
}
)
}
))
@GetMapping
public String index() {
return null;
}

record PagedObject(long id, String name) {}
record Paging(int page, int total, int lastPage) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* *
* * *
* * * *
* * * * * 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.v30.app211;

import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp211Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "index_1",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"$ref": "#/components/schemas/PagedObject"
}
},
"paging": {
"$ref": "#/components/schemas/Paging"
}
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"PagedObject": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
}
},
"Paging": {
"type": "object",
"properties": {
"page": {
"type": "integer",
"format": "int32"
},
"total": {
"type": "integer",
"format": "int32"
},
"lastPage": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}