question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support relative path in @ExampleObject

See original GitHub issue

Having a file in /src/main/resources/static/swagger/example.json. Placed inside /static because spring can expose those to the web.

It should be possible to reference the file relative to the application itself. Eg as follows:

@Operation(responses = @ApiResponse(content = @Content(examples = @ExampleObject(externalValue = "swagger/example.json")))

But even if I can access the resource via locahost:8080/swagger/example.json, when adding the url to @ExampleObject it is still not resolved:

@Operation(responses = @ApiResponse(content = @Content(examples = @ExampleObject(externalValue = "locahost:8080/swagger/example.json")))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
gitkidmkcommented, Oct 29, 2022

I’m facing same issue and i got an idea from springdoc-openapi project.

image

In this project, it create example entry(http500Example) and add Example through openApiCustomiser

actually, this code represent ref in setValue but i put json file in Example object

here is my code

package com.semtax.application.config.swagger.exampleObject;

import io.swagger.v3.oas.models.examples.Example;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

@Component
public class SwaggerExampleObject {

    @Value("classpath:/swagger/login.json")
    Resource loginResource;

    @Bean
    public Map.Entry<String, List<Example>> loginExample() throws IOException, ParseException, IllegalAccessError {

        JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(loginResource.getInputStream(), "UTF-8"));

        List<Example> ExampleList = new ArrayList<>();
        AbstractMap.SimpleEntry<String, List<Example>> loginExampleMap = new AbstractMap.SimpleEntry<>("login", null);

        for (Object s : json.keySet()){
            Example example = new Example();
            example.setValue(json.get(s));
            example.setDescription(s.toString());
            ExampleList.add(example);
        }
        loginExampleMap.setValue(ExampleList);

        return loginExampleMap;
    }
}

package com.semtax.application.config.swagger;

@OpenAPIDefinition(
        info = @Info(title = "API 명세서",
                version = "v1"))
@Configuration
@RequiredArgsConstructor
public class Swagger2Config {

    @Autowired
    SwaggerExampleObject swaggerExampleObject;

    @Bean
    public OpenApiCustomiser openApiCustomiser(Collection<Entry<String, List<Example>>> examples) {
        return openAPI -> {
            examples.forEach(example -> {
                for(Example e : example.getValue()){
                    openAPI.getComponents().addExamples(e.getDescription(), e);
                }
            });
        };
    }

}
@ApiResponse(content = @Content(examples = @ExampleObject(name = "200", ref = "#/components/examples/login200")))
1reaction
springdoccommented, Aug 12, 2019

Hi,

Tested with springdoc-openapi: 1.1.15. In order to add the examples reference, in the generated yaml/json documentation, you need to add the name of the ExampleObject:

responses= @ApiResponse(content = @Content(examples = @ExampleObject(name = "toto", externalValue = "http://localhost:8080/test/example.json")))

Displaying the example on the swagger-ui, is not a springdoc-openapi issue, but depends on a pending issue on swagger-ui side:

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 3.0.3 - Swagger
This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI...
Read more >
How to construct a relative path in Java from two absolute ...
tl;dr answer: Paths.get(startPath).relativize(Paths.get(endPath)).toString() (which, by the way, seems to be working just fine with ...
Read more >
Export Relative or Absolute Paths - TechDocs
When you initiate an export, you can select either Export Absolute Paths or Export Relative Paths. The option that you select affects the ......
Read more >
How to use JSON references ($refs) - Redocly
Within a separate file we need a relative path to the file and then the JSON Pointer path to the object separated by...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
The OpenAPI document MUST contain at least one paths field, a components field ... This URL supports Server Variables and MAY be relative, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found